Skip to main content
The TRUNCATE 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.
The “ statement performs a schema change. For more information about how online schema changes work in CockroachDB, see .
TRUNCATE is a schema change, and as such is not transactional. For more information about how schema changes work, see .

Synopsis

truncate syntax diagram

Required privileges

The user must have the DROP on the table.

Parameters

ParameterDescription
table_nameThe name of the table to truncate.
CASCADETruncate all tables with 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 dependencies on it.

Viewing schema changes

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

Examples

Truncate a table (no foreign key dependencies)

> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
|  1 | foo  |
|  2 | bar  |
+----+------+
(2 rows)
> TRUNCATE t1;
> SELECT * FROM t1;
+----+------+
| id | name |
+----+------+
+----+------+
(0 rows)

Truncate a table and dependent tables

In these examples, the orders table has a 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

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

Truncate dependent tables explicitly

> TRUNCATE customers, orders;
> SELECT * FROM customers;
+----+-------+
| id | email |
+----+-------+
+----+-------+
(0 rows)
> SELECT * FROM orders;
+----+----------+------------+
| id | customer | orderTotal |
+----+----------+------------+
+----+----------+------------+
(0 rows)

See also