Stop 09 · Modifying DataTopics · 5/9

DELETE vs TRUNCATE vs DROP

Part of the SQL Roadmap

In short

Three commands remove things, and mixing them up can cost you a whole table. Knowing exactly what each one removes, and whether you can undo it, is basic database safety.

DELETE removes chosen rows and can be filtered, TRUNCATE quickly removes all rows but keeps the table, and DROP removes the table itself.

The jaadu stands in a classroom holding a duster in one hand and a screwdriver in the other, half of the whiteboard wiped clean behind it.

Think of it like this

The classroom whiteboard

Wiping off just two wrong sums with a duster is DELETE. Wiping the entire board clean but leaving it on the wall for the next class is TRUNCATE. Unscrewing the board and carrying it out of the room is DROP. Each one removes more than the last.

Watch it happen

How much each command removes

Remove something
DELETE
Chosen rows
TRUNCATE
All rows, table stays
DROP
Rows + structure + indexes

1/4Three commands, three levels of removal.

The big ideas

What you need to know

Only DELETE filters

Need to remove some rows? DELETE with WHERE is the only one of the three that can choose.

TRUNCATE is fast

It skips row-by-row work, so it clears millions of rows quickly. In PostgreSQL, add RESTART IDENTITY to reset IDs too.

Undo depends on the database

In PostgreSQL all three can be rolled back inside a transaction. In MySQL, TRUNCATE and DROP commit immediately.

Foreign keys get in the way

You can't TRUNCATE or DROP a table other tables point to unless you add CASCADE, which affects them too.

See it in code

Walk through the code

sql
1DELETE FROM user_sessions2WHERE last_seen < NOW() - INTERVAL '30 days';3 4TRUNCATE TABLE staging_orders RESTART IDENTITY;5 6DROP TABLE IF EXISTS import_backup_2025;

1/3Remove only sessions older than 30 days. Everything else stays.

One of each in PostgreSQL. In MySQL write INTERVAL 30 DAY without quotes, and TRUNCATE always resets AUTO_INCREMENT. In SQL Server, use DATEADD(day, -30, GETDATE()).

Save yourself the pain

Common mistakes

Oops

Using DROP TABLE when you only wanted to clear the data.

Do this instead

Use TRUNCATE to empty a table and keep its structure. DROP means you'll have to recreate it.

Oops

Assuming TRUNCATE can be rolled back everywhere.

Do this instead

It can in PostgreSQL and SQL Server inside a transaction, but MySQL commits it at once. Check before you rely on it.

Your mission

Three ways to remove

~15 min

Quick check

You want an empty table with the same columns, as fast as possible. Which command fits?

Go deeper

  1. 1Delete rows matching a condition and confirm the count first.
  2. 2Check whether TRUNCATE can be rolled back in your database.
  3. 3Learn why many teams prefer a soft delete flag over real deletes.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge