DEV Community

Cover image for DELETE vs TRUNCATE vs DROP: Which One Removes What
Abdeldjalil Chougui
Abdeldjalil Chougui

Posted on Originally published at abdeldjalilchougui.biz

DELETE vs TRUNCATE vs DROP: Which One Removes What

All three commands make data disappear, and all three can ruin your day if you pick the wrong one. The difference is in what they remove and how:

  • DELETE removes some or all rows, one by one.
  • TRUNCATE removes all rows at once, keeping the empty table.
  • DROP removes the table itself: rows, structure, indexes and all.
DELETE FROM sessions WHERE expires_at < now();   -- some rows
TRUNCATE TABLE staging_import;                   -- all rows, table stays
DROP TABLE old_reports;                          -- the table is gone
Enter fullscreen mode Exit fullscreen mode

Side by side

DELETE TRUNCATE DROP
Removes Rows matching WHERE All rows Table and its data
Table structure Kept Kept Removed
WHERE clause Yes No No
Speed on big tables Slow, row by row Fast Fast
Row triggers fire Yes No No
Rollback Yes, in a transaction PostgreSQL, SQL Server: yes. MySQL, Oracle: no PostgreSQL, SQL Server: yes. MySQL, Oracle: no
Identity / auto-increment Not reset Reset in MySQL and SQL Server; optional in PostgreSQL Gone with the table
Command type DML DDL DDL

DELETE

DELETE is a normal data operation. It finds the rows that match the WHERE clause and removes them one at a time. For each row it:

  • writes to the transaction log, so it can be rolled back and replicated,
  • fires any ON DELETE row triggers,
  • checks foreign keys and applies ON DELETE CASCADE or SET NULL rules,
  • updates every index on the table.

That makes it safe and flexible, and slow on large tables. Deleting 50 million rows in one statement creates a huge transaction, holds locks for a long time, and can grow the log a lot.

Also, the space is not always returned right away. In PostgreSQL, deleted rows stay on disk as dead tuples until VACUUM cleans them; the file usually does not shrink. In MySQL InnoDB, freed space is reused inside the table but the file does not shrink by itself either.

Without a WHERE clause, DELETE FROM t removes every row, the slow way.

TRUNCATE

TRUNCATE empties a table by deallocating its storage instead of deleting rows individually. It does not scan the rows, so it is fast whatever the table size, and it frees space immediately.

The details differ by database, which is where people get surprised:

  • Rollback: in PostgreSQL and SQL Server, TRUNCATE inside a transaction can be rolled back. In MySQL and Oracle it causes an implicit commit and cannot be undone with ROLLBACK.
  • Identity: MySQL resets AUTO_INCREMENT and SQL Server resets the identity seed. PostgreSQL keeps sequences as they are unless you write TRUNCATE ... RESTART IDENTITY.
  • Triggers: row-level DELETE triggers do not fire. PostgreSQL has separate TRUNCATE triggers if you need them.
  • Foreign keys: you usually cannot truncate a table that other tables reference. PostgreSQL refuses unless you add CASCADE (which also truncates the referencing tables). SQL Server refuses while a foreign key points to it. MySQL InnoDB refuses as long as foreign key checks are on.
  • Locks: it takes a strong table lock (in PostgreSQL an ACCESS EXCLUSIVE lock), so it waits for, and blocks, all other queries on that table.

Typical uses: clearing staging tables before a reload, resetting test data, emptying a log table.

DROP

DROP TABLE removes the table definition and everything attached to it: data, indexes, constraints, triggers and permissions. Anything that refers to the table breaks.

  • In PostgreSQL, DROP TABLE fails if views or foreign keys depend on it, unless you add CASCADE, which drops those dependent objects too. Read what CASCADE will remove before you run it.
  • DROP TABLE IF EXISTS avoids an error when the table is not there, useful in migration scripts.
  • Rollback follows the same rule as TRUNCATE: possible inside a transaction in PostgreSQL and SQL Server, an implicit commit in MySQL. Oracle can restore a dropped table from its recycle bin with FLASHBACK TABLE when that feature is enabled, but do not count on it as a backup.

The same verb removes other objects: DROP INDEX, DROP VIEW, DROP DATABASE.

Common mistakes

  • DELETE without WHERE in production because a line got cut when pasting. Run the SELECT with the same WHERE first, or work inside a transaction you can roll back.
  • Assuming TRUNCATE can be rolled back everywhere. In MySQL it cannot.
  • Deleting millions of rows in one statement. Delete in batches (for example 10,000 rows per transaction), or use partitioning and drop old partitions.
  • Expecting the disk to shrink after DELETE. It often does not without VACUUM FULL or a table rebuild.
  • DROP ... CASCADE without checking what depends on the table.
  • Relying on TRUNCATE to reset IDs in PostgreSQL without RESTART IDENTITY.

When to pick which

  • DELETE when you remove some rows, need triggers and cascades to run, or need the change logged and replicated row by row.
  • TRUNCATE when you want an empty table fast and nothing references it, such as staging or test tables.
  • DROP when the table should not exist anymore.
  • For data that expires by time (logs, events), consider partitioning by date and dropping old partitions: it is as fast as DROP and leaves the rest of the table alone.

Whichever you pick, have a recent backup you have actually tested restoring.

Watch the animated version: https://youtu.be/u1upwBLAggo

In short

DELETE removes rows, carefully and slowly. TRUNCATE empties the table, quickly and with fewer safety nets. DROP removes the table itself. Check how your database handles rollback, identity and foreign keys before relying on any of them.


Originally published at abdeldjalilchougui.biz.

Top comments (0)