Deleting Data (DELETE vs TRUNCATE vs DROP)
Choose DELETE, TRUNCATE, or DROP based on recovery needs, speed, and schema impact.
In plain words
DELETE removes some or all rows, TRUNCATE removes all rows very fast, DROP removes the table completely.
Think of it like…
DELETE is taking files out of a cabinet one by one and logging each; TRUNCATE is emptying the whole cabinet in one go; DROP is throwing the cabinet out.
Where you’ve seen it
Clearing a 50-million-row staging table every night: DELETE FROM staging logs 50 million row deletions and, in PostgreSQL, leaves dead rows for VACUUM; TRUNCATE staging finishes in milliseconds and frees the space immediately.
Words you’ll hear
- DML vs DDL
- DELETE is DML (changes data); TRUNCATE and DROP are DDL (change storage or structure).
- Rollback
- Undoing changes made inside an open transaction.
- Identity reset
- TRUNCATE … RESTART IDENTITY resets serial counters in PostgreSQL; DELETE never does.
- Lock
- TRUNCATE and DROP take an exclusive table lock; DELETE locks the rows it touches.
Visual diagram
Theory & example queries
What each command actually does
DELETE finds the matching rows and removes them one at a time, writing each change to the transaction log and firing row-level triggers. In PostgreSQL the old row versions stay on disk until VACUUM cleans them up, so a big DELETE doesn't shrink the table right away.
TRUNCATE skips the row-by-row work and releases the table's data pages in one step, which is why it takes milliseconds regardless of size. DROP removes the table definition as well, along with its indexes, constraints, triggers and grants. Views and code that use the table break.
| id | event_type |
|---|---|
| 1 | login |
| 2 | click |
| 3 | logout |
Three representative rows are shown; the lesson discusses a 50-million-row table.
| id | event_type |
|---|---|
| No rows | |
Both commands leave the table empty. DELETE processes rows; TRUNCATE deallocates whole table data.
DELETE FROM events;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
TRUNCATE events;PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Expected output
Note: Statuses for the stated 50-million-row sample; command duration, locking and WAL volume are workload-dependent.
on 50 million rows can run for minutes and bloat the table;
finishes almost instantly.
Key takeaway: DELETE is selective and row-by-row; TRUNCATE is all-or-nothing and fast.
Rollback, triggers and foreign keys
DELETE can always be rolled back inside a transaction. TRUNCATE can be rolled back in PostgreSQL and SQL Server when it runs inside BEGIN … ROLLBACK, but MySQL and Oracle commit it implicitly, so there's no undo.
You usually can't TRUNCATE a table that other tables reference with foreign keys. PostgreSQL requires TRUNCATE … CASCADE, which also empties the referencing tables. For identity columns, PostgreSQL keeps counting unless you add RESTART IDENTITY, while MySQL and SQL Server reset the counter.
| id | created_at |
|---|---|
| 701 | 2026-05-01 |
| 702 | 2026-08-18 |
| 703 | 2026-11-20 |
| id | created_at |
|---|---|
| 702 | 2026-08-18 |
| 703 | 2026-11-20 |
The old May row is removed while recent rows remain.
DELETE FROM logsWHERE id IN ( SELECT id FROM logs WHERE created_at < now() - interval '90 days' LIMIT 10000 );PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Expected output
Note: Representative sequence: full batches continue until a final pass finds no eligible rows; real batch counts depend on data volume.
Delete in batches,
, repeated until it affects 0 rows. Locks and log volume stay small.
Key takeaway: "Can I roll it back?" depends on both the command and the database.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Do you need a WHERE?
If you're removing only some rows, DELETE is the only option. TRUNCATE and DROP always affect the whole table.
DELETE FROM ordersWHERE status = 'test'When to use it & trade-offs
What it is
DELETE removes rows (all of them, or only those matching a WHERE) one at a time and fires triggers. TRUNCATE empties the whole table in one operation by releasing its storage. DROP removes the table itself: structure, data, indexes, constraints and permissions.
Why it matters: Picking the wrong one is how people lose production data or lock a busy table for minutes. It's also a top interview question because the differences (WHERE support, speed, rollback, triggers, identity reset) touch several core concepts at once.
When to reach for it
- DELETE for retention jobs and removing specific bad records
- TRUNCATE for nightly reloads of staging tables
- DROP when retiring a table after a migration
When to avoid or be careful
- DELETE without WHERE on a huge table when TRUNCATE would do
- TRUNCATE … CASCADE without checking which tables it will empty
- DROP in production without a verified backup
DELETE vs TRUNCATE vs DROP
Strengths: WHERE clause, fires triggers, always rollback-able.
Watch out: Slow on big tables, logs every row, leaves dead rows in PostgreSQL.
Strengths: Near-instant, frees space immediately, can reset identity.
Watch out: No WHERE, no row triggers, blocked by foreign keys, no rollback in MySQL or Oracle.
Strengths: Removes data, structure, indexes and grants in one step.
Watch out: Irreversible without a backup; breaks views and code that use the table.
Interview signals & numbers to know
When this topic comes up
- “DELETE vs TRUNCATE vs DROP”
- “Can you roll back a TRUNCATE?”
- Emptying a large table quickly
- Resetting an auto-increment id
- Audit triggers
Benchmarks & rules of thumb
- DELETE
- DML: always rollback-able
- TRUNCATE
- DDL: rollback-able in PostgreSQL and SQL Server only
- DROP
- DDL: the table is gone
Common mistakes & follow-ups
Common interview mistakes
- Saying "TRUNCATE can't be rolled back" as a universal rule. It can in PostgreSQL and SQL Server.
- Running one giant DELETE on a busy table and locking it for minutes.
- Forgetting that TRUNCATE doesn't fire DELETE triggers, so audit logs never see those rows.
Likely interviewer follow-ups
- Why is TRUNCATE so much faster than DELETE?
- Can you TRUNCATE a table that other tables reference with foreign keys?
- How would you delete 100 million old rows from a live table without downtime?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Deleting Data (DELETE vs TRUNCATE vs DROP) with hands-on SQL practice and interview Q&A.