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
| Question | DELETE | TRUNCATE | DROP TABLE |
|---|---|---|---|
| What goes? | Rows you choose (or all rows) | All rows | The whole table |
| Table still exists? | Yes | Yes, empty | No |
| Can use WHERE? | Yes | No | No |
| Speed on a big table | Slower: works row by row | Fast | Fast |
| Auto-increment counter | Keeps counting from where it was | Often reset (depends on the database) | Gone with the table |
| Undo inside a transaction? | Yes, before COMMIT | Depends on the database | Depends 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:
- PostgreSQL:
TRUNCATEcan be rolled back inside a transaction. IDs keep counting unless you addRESTART IDENTITY. If other tables have foreign keys pointing at this one, you must truncate them too or addCASCADE, which is a strong command to use with care. - MySQL:
TRUNCATEcommits immediately and cannot be rolled back. It resets the auto-increment counter. It refuses to run on a table that other tables reference with foreign keys. - SQLite: there is no
TRUNCATEcommand.DELETE FROM tasks;with noWHEREis the equivalent, and SQLite speeds it up internally.
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.
- After
DELETE FROM tasks WHERE id = 2;the table has two rows, 1 and 3. The next new task usually gets ID 4. - After
DELETE FROM tasks;the table is empty but still there, and the next new task usually still gets ID 4. - After
TRUNCATE TABLE tasks;in MySQL the table is empty and the next new task gets ID 1 again. In PostgreSQL it keeps counting unless you asked forRESTART IDENTITY. - After
DROP TABLE tasks;aSELECTontasksfails, because there is no table to read.
Trying this yourself on a throwaway table is the quickest way to make the differences stick.
Which one should I use?
- Remove some rows:
DELETE ... WHERE .... - Remove all rows, keep the table, in a practice database:
TRUNCATE(orDELETE FROMin SQLite). - Remove all rows on a real app: slow down. Make a backup first, and prefer
DELETEinside a transaction if you need to be able to undo it. - Remove the table completely:
DROP TABLE, after checking nothing still uses it.
Safety habits
- Practise on a copy. Our guide to backing up your practice database before experiments takes two minutes.
- Write the
SELECTversion first and check the rows. - Use a transaction for anything destructive when your database supports it.
- 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
DROPcannot 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.