Database.Beginner.What is the difference between DELETE, TRUNCATE, and DROP?

DELETE vs TRUNCATE vs DROP

CommandWhat it DoesSpeedCan Rollback?Affects Structure?
DELETEDeletes specific rows based on WHERE condition (or all if no condition).Slow for big tables✅ Yes (if in a transaction)❌ No — table stays.
TRUNCATEDeletes all rows from the table — no WHERE clause. Faster than DELETE.⚡ Very fast🚫 No (auto-commit in many DBs)❌ No — table stays.
DROPDeletes the entire table (structure + data) permanently.⚡ Very fast🚫 No (permanent)✅ Yes — removes the table itself.

1. DELETE

  • Used to remove specific rows:
DELETE FROM Users WHERE age > 50;

You can delete all rows:

DELETE FROM Users;

Slow for big tables because it deletes row by row, firing triggers, checking constraints, writing to logs.

Can be rolled back if inside a transaction:

BEGIN;
DELETE FROM Users WHERE age > 50;
ROLLBACK; -- Undo
  • Table remains — just with fewer rows.

2. TRUNCATE

  • Removes all rows from the table — no WHERE allowed.
TRUNCATE TABLE Users;
  • Much faster than DELETE — it doesn’t scan rows individually.
  • Cannot specify conditions — it’s all-or-nothing.
  • Auto-commits in most databases — cannot rollback in most systems (depends on the DB).
  • Resets auto-increment counters in many databases.
  • Keeps the table structure — just deletes all the data.

TRUNCATE is like a quick emptying of the table.

3. DROP

  • Removes the entire table, both structure and data:
DROP TABLE Users;

Cannot rollback — it’s a hard delete.

Table is gone — you can’t query it anymore.

If there are foreign key constraints, you may have to DROP those first or disable them.

DROP is like deleting the whole file from disk.

Summary

FeatureDELETETRUNCATEDROP
Deletes specific rows?✅ Yes (WHERE)🚫 No — all rows only🚫 No — deletes whole table
Deletes all rows?✅ Yes (without WHERE)✅ Yes (always)🚫 No — deletes table entirely
Table structure remains?✅ Yes✅ Yes❌ No — table gone
Rollback possible?✅ Yes (with transaction)🚫 No (auto-commit)🚫 No
Speed🐢 Slow (row-by-row)⚡ Fast (bulk delete)⚡ Fast (object drop)
Resets auto-increment?🚫 No✅ Yes❌ (Table is deleted)

Real-Life Analogy

ActionAnalogy
DELETE rowsThrowing specific books out of a shelf.
TRUNCATE tableEmptying the whole shelf but keeping the shelf.
DROP tableThrowing the entire shelf away — no books, no shelf.

⚠️ Caution

  • TRUNCATE and DROP are usually DANGEROUS in production — because they can’t be easily undone.
  • DELETE is safer (you can control what to remove and rollback).
This entry was posted in Без рубрики. Bookmark the permalink.