TRUNCATE

The TRUNCATE statement removes all rows from a table. At a high level, it works by dropping the table and recreating a new table with the same name.

Note:

For smaller tables (with less than 1000 rows), using a DELETE statement without a WHERE clause will be more performant than using TRUNCATE.

Note:

This statement performs a schema change. For more information about how online schema changes work in CockroachDB, see Online Schema Changes.

Synopsis

TRUNCATETABLEtable_name,CASCADERESTRICT

Required privileges

The user must have the DROP privilege on the table.

Parameters

ParameterDescription
tablenameThe name of the table to truncate.
CASCADETruncate all tables with Foreign Key dependencies on the table being truncated.CASCADE does not list dependent tables it truncates, so should be used cautiously.
RESTRICT(Default)_ Do not truncate the table if any other tables have Foreign Key dependencies on it.

Limitations

TRUNCATE is a schema change, and as such is not transactional. For more information about how schema changes work, see Online Schema Changes.

Viewing schema changes

This schema change statement is registered as a job. You can view long-running jobs with SHOW JOBS.

Examples

Truncate a table (no foreign key dependencies)

  1. > SELECT * FROM t1;
  1. +----+------+
  2. | id | name |
  3. +----+------+
  4. | 1 | foo |
  5. | 2 | bar |
  6. +----+------+
  7. (2 rows)
  1. > TRUNCATE t1;
  1. > SELECT * FROM t1;
  1. +----+------+
  2. | id | name |
  3. +----+------+
  4. +----+------+
  5. (0 rows)

Truncate a table and dependent tables

In these examples, the orders table has a Foreign Key relationship to the customers table. Therefore, it's only possible to truncate the customers table while simultaneously truncating the dependent orders table, either using CASCADE or explicitly.

Truncate dependent tables using CASCADE

Warning:
CASCADE truncates all dependent tables without listing them, which can lead to inadvertent and difficult-to-recover losses. To avoid potential harm, we recommend truncating tables explicitly in most cases. See Truncate Dependent Tables Explicitly for more details.

  1. > TRUNCATE customers;
  1. pq: "customers" is referenced by foreign key from table "orders"
  1. > TRUNCATE customers CASCADE;
  1. > SELECT * FROM customers;
  1. +----+-------+
  2. | id | email |
  3. +----+-------+
  4. +----+-------+
  5. (0 rows)
  1. > SELECT * FROM orders;
  1. +----+----------+------------+
  2. | id | customer | orderTotal |
  3. +----+----------+------------+
  4. +----+----------+------------+
  5. (0 rows)

Truncate dependent tables explicitly

  1. > TRUNCATE customers, orders;
  1. > SELECT * FROM customers;
  1. +----+-------+
  2. | id | email |
  3. +----+-------+
  4. +----+-------+
  5. (0 rows)
  1. > SELECT * FROM orders;
  1. +----+----------+------------+
  2. | id | customer | orderTotal |
  3. +----+----------+------------+
  4. +----+----------+------------+
  5. (0 rows)

See also

Was this page helpful?
YesNo