DELETE vs TRUNCATE vs DROP: What Happens Inside

DELETE marks and logs every row and holds locks. TRUNCATE and DROP release whole files. Why big deletes are slow and how partitions help.

In this chapter

DELETE is slow, DROP is not: how data really leaves

DELETE vs TRUNCATE vs DROP: common questions

What is the difference between DELETE, TRUNCATE and DROP?

DELETE removes chosen rows one by one, can take a WHERE clause, fires triggers and logs each row. TRUNCATE empties the whole table quickly and keeps its structure. DROP removes the table itself.

Can TRUNCATE be rolled back?

In PostgreSQL and SQL Server, yes, inside a transaction. In MySQL, no: TRUNCATE commits implicitly.

Does TRUNCATE reset the auto-increment or identity counter?

In MySQL and SQL Server, yes. In PostgreSQL, only if you write TRUNCATE ... RESTART IDENTITY; the default keeps the sequence.

Does TRUNCATE fire DELETE triggers?

No. Row-level DELETE triggers do not fire. PostgreSQL has separate ON TRUNCATE triggers.

More on Life of a statement

Keep learning

Overview

chapters · life of a statement DELETE is slow, DROP is not: how data really leaves

1. A delete is a write

The row cannot vanish at once: rollback needs it, and so do snapshots that started earlier (MVCC). So DELETE pays the full write path per row.

2. Rows versus files

Drop a partition: the same trick for one slice, if you partitioned by the expiry column (partitioning).

3. Interview: DELETE vs TRUNCATE vs DROP

A common interview question (shared by @aslambeg84) The question: a 50-million-row app_logs table has to be emptied tonight. The application keeps writing to it afterwards, so the table must stay. Which command do you use? Show the answer TRUNCATE. It empties the whole table almost instantly and keeps its structure, indexes and permissions. DELETE would work but runs row by row through the full write path. DROP removes the table itself, so the next insert from the app fails. The three commands side by side Chosen rows Every row The table: rows, definition, indexes, triggers WHERE clause Row by row, each one logged Releases the data files and starts empty ones Releases the files and the definition Row-level DELETE triggers Fire for every row Do not fire Do not fire (they are dropped too) Grows with rows × indexes About constant Table afterwards Still there Still there, empty

The detail that separates strong answers: TRUNCATE differs by database

TRUNCATE in three engines MySQL (InnoDB) SQL Server Inside a transaction Can be rolled back ID counter Kept, unless you write RESTART IDENTITY AUTO_INCREMENT goes back to its start IDENTITY goes back to its seed No DELETE triggers Referenced by a foreign key Fails unless you add CASCADE or truncate those tables too ACCESS EXCLUSIVE: blocks reads too, for a moment Metadata lock, like a DDL statement Schema modification lock

When to use each

DELETE when you remove some rows, need triggers or cascades to run, or must be able to roll back on any engine. For millions of rows, delete in batches (section 5). TRUNCATE when the whole table goes but the table stays: staging tables, logs, test data. Check the foreign keys and whether the ID counter should restart. DROP when the table itself is no longer needed. Dropping one old partition is the everyday version of this. ★ Say this in the interview

4. Mark, commit, clean up, and the file that stays big

Cleanup waits for the oldest snapshot. One long-open transaction stops it and the table grows (purge interview). Freed space stays in the file, reusable by new rows of that table, not returned to the OS. Only a rebuild shrinks it. It copies live rows to a new file, needs spare disk, and may block writes unless done online.

5. Why one big DELETE hurts everybody

Deleting 50 million rows in one statement is one transaction: every lock held to the end, old copies of every row, a flood of log, and a replica that replays it as one block. Delete in small committed batches instead (transaction misuse). Index the filter column, or every round re-scans. Slow down automatically when replica lag climbs.

6. Ways to avoid the big delete

Making data "go away", compared Cost at removal Space back to OS? One DELETE Rows × indexes, one transaction Locks, old copies, replica lag Batched DELETE Same work, spread out Soft delete (deleted_at) One small UPDATE No, data stays Archive, then delete Copy plus batched delete After a rebuild Two places to keep consistent Time partitions, drop old Almost none TRUNCATE / DROP Whole table only A common mature design: soft delete for user-facing undo, time partitions for bulk expiry, a scheduled purge job for legal erasure.

Recap

Big deletes: small committed batches with a pause. TRUNCATE, DROP and partition drops release files in constant time. ⚙ In real engines

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.