DROP TABLE vs TRUNCATE TABLE in PostgreSQL: What's the Difference?
Learn the difference between DROP TABLE and TRUNCATE TABLE in PostgreSQL, when to use CASCADE, and why TRUNCATE is faster than deleting every row.
DROP TABLE vs TRUNCATE TABLE
Both remove data, but they do very different things.
DROP TABLE — Delete the Table Entirely
Removes the table's structure and all its data — the table stops existing.
DROP TABLE employees;
DROP TABLE IF EXISTS employees; -- no error if it's already gone
If another table has a foreign key pointing at this one, DROP TABLE fails unless you cascade the drop:
DROP TABLE customers CASCADE;
-- also drops any foreign key constraints (and dependent views) referencing customers
TRUNCATE TABLE — Empty the Table, Keep the Structure
Removes every row but leaves the table itself (columns, constraints, indexes) intact — ready to be filled with new data immediately.
TRUNCATE TABLE employees;
- It's far faster than
DELETE FROM employees;for large tables, since it doesn't scan or log row-by-row. - It also resets any
SERIAL/identity sequence back to its starting value:
TRUNCATE TABLE employees RESTART IDENTITY;
- Truncating a table referenced by a foreign key needs
CASCADEtoo, unless the referencing table is empty:
TRUNCATE TABLE customers CASCADE;
Advertisement728 x 90
Quick Comparison
DROP TABLE |
TRUNCATE TABLE |
DELETE FROM |
|
|---|---|---|---|
| Removes data | Yes | Yes | Yes (optionally filtered) |
| Removes table structure | Yes | No | No |
| Speed on large tables | N/A | Very fast | Slower |
| Resets auto-increment IDs | N/A | With RESTART IDENTITY |
No |
Can filter with WHERE |
No | No | Yes |
Common Errors & Troubleshooting
ERROR: cannot truncate a table referenced in a foreign key constraint— addCASCADEto also truncate the referencing tables, or truncate the child tables first, in dependency order.- Realizing too late that
TRUNCATEisn't transactional the way you expected — it IS transactional (rollback-able insideBEGIN/COMMIT) in PostgreSQL, unlike some other databases — but it still can't be filtered with aWHEREclause, so don't reach for it when you only meant to delete some rows. DROP TABLEfailing with "cannot drop table because other objects depend on it" — a view or foreign key elsewhere depends on this table; either drop those first, or addCASCADEif removing them too is intended.
Key Takeaways
DROP TABLEremoves the table itself;TRUNCATEempties it but keeps its structure.TRUNCATEis dramatically faster thanDELETE FROM tablewith noWHEREclause, on large tables.- Both may need
CASCADEif other tables have foreign keys pointing at the one you're removing.
Advertisement320 x 100