TRUNCATE TABLE instantly removes all rows from one or more tables and optionally resets identity sequences, bypassing row-level locks and logging for high-speed cleanup.
TRUNCATE TABLE is bulk, metadata-level deletion. PostgreSQL deallocates data pages instead of logging every row, making it dramatically faster and less WAL-intensive than DELETE for large datasets.
Use TRUNCATE TABLE table_name;
to wipe data instantly. Add options such as RESTART IDENTITY
, CASCADE
, or multiple table names for more control.
Add RESTART IDENTITY
to zero out sequence generators so new rows start at 1. Omitting it keeps sequence values untouched.
Use CASCADE
when child tables reference the target through foreign keys. PostgreSQL truncates dependent tables in the same transaction, preventing constraint errors.
Yes. List table names comma-separated. PostgreSQL locks and truncates them atomically, ideal for clearing staging schemas after ETL jobs.
BEGIN;
TRUNCATE TABLE Orders, OrderItems RESTART IDENTITY CASCADE;
COMMIT;
This script erases historical order data, resets order IDs, and removes dependent order items—all in milliseconds.
Wrap TRUNCATE in a transaction so you can ROLLBACK if needed. Always run on non-production first, verify row counts, and back up critical tables.
No. Restrict the privilege to roles that truly need it. TRUNCATE is irreversible once committed.
DELETE supports WHERE clauses, fires row-level triggers, and returns affected row count. TRUNCATE is all-or-nothing, bypasses row triggers, and is non-recoverable without backups.
No. Row-level triggers do not execute, but statement-level triggers (AFTER TRUNCATE
) can.
Yes. TRUNCATE is fully WAL-logged in PostgreSQL 13+. Replication subscribers receive the command, not individual row deletions.
Only if the command is still inside an open transaction. After COMMIT, data recovery requires backups.