🐘 PostgreSQL Tutorial

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 CASCADE too, unless the referencing table is empty:
TRUNCATE TABLE customers CASCADE;

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 — add CASCADE to also truncate the referencing tables, or truncate the child tables first, in dependency order.
  • Realizing too late that TRUNCATE isn't transactional the way you expected — it IS transactional (rollback-able inside BEGIN/COMMIT) in PostgreSQL, unlike some other databases — but it still can't be filtered with a WHERE clause, so don't reach for it when you only meant to delete some rows.
  • DROP TABLE failing 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 add CASCADE if removing them too is intended.

Key Takeaways

  • DROP TABLE removes the table itself; TRUNCATE empties it but keeps its structure.
  • TRUNCATE is dramatically faster than DELETE FROM table with no WHERE clause, on large tables.
  • Both may need CASCADE if other tables have foreign keys pointing at the one you're removing.