TRUNCATE TABLE instantly removes all rows from one or more tables, optionally resetting sequences and cascading to related tables.
TRUNCATE TABLE instantly deletes every row in the specified table(s). PostgreSQL drops and recreates the data pages, so the action is nearly constant-time, uses little WAL, and releases disk space immediately after commit.
DELETE scans the table, logs each removed row, and fires DELETE triggers. TRUNCATE logs only the fact that the entire relation was removed, bypasses row-level triggers, and acquires an ACCESS EXCLUSIVE lock.
Use the keyword TABLE optionally, list one or more tables, and pick identity and FK behaviours:
TRUNCATE TABLE table1 [, table2]
[RESTART IDENTITY | CONTINUE IDENTITY]
[CASCADE | RESTRICT];
Yes—comma-separate them. PostgreSQL handles each with the same lock level, ensuring consistency and speed when purging several staging tables together.
Use CASCADE only when child tables should be truncated automatically. Without it, PostgreSQL errors if foreign keys reference the target. RESTRICT (default) protects accidental data loss.
RESTART IDENTITY resets any sequences owned by the truncated tables to their start values, handy for demo or test databases. CONTINUE IDENTITY leaves sequences untouched to avoid PK conflicts with replicas.
TRUNCATE TABLE Orders, OrderItems
RESTART IDENTITY CASCADE;
The command wipes all demo sales data, truncates the child OrderItems automatically, and restarts both tables’ sequences at 1.
Run it in maintenance windows or short transactions to minimise blocking. Always confirm you are in the correct database. Keep backups or snapshots if accidental loss is unacceptable.
Forgetting CASCADE causes FK violations; holding the lock too long blocks writers. See detailed fixes below.
Yes, you can. The data is removed instantly, but changes become visible only after COMMIT. ROLLBACK restores the table.
Row-level triggers do not fire; statement-level triggers (AFTER TRUNCATE
) do. Add auditing logic there if needed.
GRANT a role the TRUNCATE
privilege or give them table ownership. Without it, superusers only can truncate.
Yes. PostgreSQL deallocates whole data pages rather than logging each row deletion, so even multi-million-row tables finish in milliseconds.
Absolutely. If you issue TRUNCATE inside a transaction block, a later ROLLBACK restores the rows because the data pages were not yet committed.
Only when you specify RESTART IDENTITY. Otherwise, the underlying sequence continues from its last value.