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:
-
DELETEremoves some or all rows, one by one. -
TRUNCATEremoves all rows at once, keeping the empty table. -
DROPremoves 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 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 DELETErow triggers, - checks foreign keys and applies
ON DELETE CASCADEorSET NULLrules, - 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,
TRUNCATEinside a transaction can be rolled back. In MySQL and Oracle it causes an implicit commit and cannot be undone withROLLBACK. - Identity: MySQL resets
AUTO_INCREMENTand SQL Server resets the identity seed. PostgreSQL keeps sequences as they are unless you writeTRUNCATE ... RESTART IDENTITY. - Triggers: row-level
DELETEtriggers do not fire. PostgreSQL has separateTRUNCATEtriggers 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 EXCLUSIVElock), 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 TABLEfails if views or foreign keys depend on it, unless you addCASCADE, which drops those dependent objects too. Read whatCASCADEwill remove before you run it. -
DROP TABLE IF EXISTSavoids 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 withFLASHBACK TABLEwhen 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
-
DELETEwithoutWHEREin production because a line got cut when pasting. Run theSELECTwith the sameWHEREfirst, or work inside a transaction you can roll back. - Assuming
TRUNCATEcan 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 withoutVACUUM FULLor a table rebuild. -
DROP ... CASCADEwithout checking what depends on the table. - Relying on
TRUNCATEto reset IDs in PostgreSQL withoutRESTART IDENTITY.
When to pick which
-
DELETEwhen you remove some rows, need triggers and cascades to run, or need the change logged and replicated row by row. -
TRUNCATEwhen you want an empty table fast and nothing references it, such as staging or test tables. -
DROPwhen 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
DROPand 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.