DELETE vs TRUNCATE vs DROP: What Each One Removes

DELETE removes rows from a table, either the ones your WHERE clause picks or all of them, and leaves the table itself in place. TRUNCATE removes every row at once, usually much faster, and also leaves the empty table. DROP TABLE removes the whole table: its rows, its columns and its definition, so it no longer exists. A simple picture helps: DELETE rubs out some lines on a page, TRUNCATE rips out every written line but keeps the empty page, and DROP throws away the page. The details, such as whether you can undo it and what happens to auto-increment IDs, depend on the database you use.

The three commands side by side

QuestionDELETETRUNCATEDROP TABLE
What goes?Rows you choose (or all rows)All rowsThe whole table
Table still exists?YesYes, emptyNo
Can use WHERE?YesNoNo
Speed on a big tableSlower: works row by rowFastFast
Auto-increment counterKeeps counting from where it wasOften reset (depends on the database)Gone with the table
Undo inside a transaction?Yes, before COMMITDepends on the databaseDepends on the database

DELETE: the careful one

-- remove one finished task
DELETE FROM tasks WHERE id = 42;

-- remove every task (the table stays)
DELETE FROM tasks;

DELETE is the command you will use most. It checks each row against the WHERE clause, so you can be precise. It also works inside a transaction: if you start one with BEGIN, you can look at the result and ROLLBACK if it is wrong. Our page what CRUD leaves out introduces transactions gently.

The famous beginner mistake is forgetting the WHERE clause and deleting every row. A good habit is to write the WHERE first as a SELECT, check which rows come back, then change SELECT * to DELETE. If it has already happened, see when an UPDATE or DELETE changes the wrong rows.

Notice that deleting rows does not reset an auto-increment ID. If your last task had ID 10 and you delete everything, the next new task usually gets ID 11. That surprises many beginners, and it is normal; our page on auto-increment IDs after you delete a row explains why IDs are labels, not counts.

TRUNCATE: empty the table quickly

TRUNCATE TABLE tasks;

TRUNCATE empties a table in one step instead of row by row, which is why it is fast on large tables. It cannot take a WHERE clause: it is all or nothing. Databases differ in the details:

Foreign keys are explained in how to add a foreign key between two tables.

DROP TABLE: remove the table itself

DROP TABLE tasks;

-- avoid an error if it is already gone
DROP TABLE IF EXISTS tasks;

DROP TABLE deletes the data and the structure. Any query that mentions tasks afterwards fails with a "no such table" style error until you create it again. In PostgreSQL a DROP inside a transaction can still be rolled back before you commit; in MySQL it commits straight away. You will mostly use DROP while practising, for example to start a table again with a different design, or in a schema migration, which is explained in what a schema migration is.

A worked comparison on one table

Here is the same small practice table treated three ways, so you can see the difference in what is left behind. Start with a tasks table holding three rows with IDs 1, 2 and 3.

Trying this yourself on a throwaway table is the quickest way to make the differences stick.

Which one should I use?

Safety habits

  1. Practise on a copy. Our guide to backing up your practice database before experiments takes two minutes.
  2. Write the SELECT version first and check the rows.
  3. Use a transaction for anything destructive when your database supports it.
  4. Do not use an all-powerful admin account in your app. Give the app its own database user with only the permissions it needs. An app account without permission to DROP cannot drop a table by accident, even if a bug or an attacker tries.

Frequently asked questions

Is TRUNCATE faster than DELETE?

Usually, yes, on large tables, because it empties the table in one step instead of row by row. It cannot remove only some rows.

Can I undo a TRUNCATE?

It depends on the database. In PostgreSQL it can be rolled back inside a transaction before you commit. In MySQL it commits immediately and cannot be rolled back.

Does DELETE reset the ID counter?

No. After a DELETE, new rows usually continue from the last ID used. TRUNCATE resets it in some databases, such as MySQL.

Keep learning from the beginner hub.