TRUNCATE
What TRUNCATE does — and what it doesn't
TRUNCATE TABLE t removes all rows from a table while keeping the table
itself — its columns, data types, indexes, and constraints all remain. (The earlier claim that it "removes
rows and columns" is wrong: columns are part of the structure, and the structure is retained — that's the whole point
versus DROP.)
Why it's fast, and the rollback nuance
TRUNCATE is fast not "because it only deals with data" but because of how it removes that data: instead of
deleting and logging each row (as DELETE does), it deallocates the table's data pages wholesale
with minimal logging, and resets the AUTO_INCREMENT counter. It takes no WHERE clause (all or
nothing) and does not fire per-row triggers.
Rollback is engine-specific — a real gotcha:
- MySQL (InnoDB) and Oracle:
TRUNCATEis DDL and implicitly commits, so it cannot be rolled back. Treat it as irreversible. - PostgreSQL and SQL Server:
TRUNCATEis transactional — run it inside a transaction andROLLBACKundoes it.
| TRUNCATE | DELETE | DROP | |
|---|---|---|---|
| Removes | all rows | rows (WHERE) | the whole table |
| Keeps structure? | yes | yes | no |
| Class | DDL | DML | DDL |
| Rollback (MySQL) | no | yes | no |
Takeaways
- TRUNCATE empties a table but keeps columns/indexes/constraints; it does not remove columns.
- Fast because it deallocates pages with minimal logging (not per-row), and resets AUTO_INCREMENT.
- Rollback depends on the engine: irreversible in MySQL/Oracle, transactional in PostgreSQL/SQL Server.
Re-authored for correctness for this guide (the prior version said TRUNCATE removes columns and could always be rolled back). Per MySQL, PostgreSQL, Oracle & SQL Server docs. See also: DELETE, What is SQL (DDL vs DML).
🎯 STRICT STANDOUT: Why / mental model / when-not / worked / failure / hostile panel — TRUNCATE
Why this concept exists (judgment layer)
TRUNCATE is page-deallocation DDL (usually), not 'fast DELETE'. Engine-specific rollback and FK refusal are the production landmines — MySQL cannot undo it.
Mental model (install this intuition)
TRUNCATE keeps structure, drops all rows, minimal logging, resets identity, skips row triggers. MySQL/Oracle: implicit commit, no ROLLBACK. Postgres/SQL Server: transactional TRUNCATE. Refuses if FK references remain (unless CASCADE).
Worked example with numbers or traced steps
DELETE 10M rows: row locks + undo + VACUUM later; identity unchanged
TRUNCATE: deallocate pages; AUTO_INCREMENT → 1; seconds not hours
MySQL: BEGIN; TRUNCATE t; ROLLBACK; -- still empty (already committed)
Postgres: BEGIN; TRUNCATE t; ROLLBACK; -- rows back
FK child exists → ERROR unless TRUNCATE … CASCADE
When NOT to use / named alternative
Need WHERE filter, row triggers, or safe rollback on MySQL → DELETE. Need table definition gone → DROP. Need partial purge → batched DELETE or partition DROP.
Failure mode & ops fingerprint
Fingerprint: engineer wraps TRUNCATE in transaction on MySQL expecting undo; prod table emptied permanently; TRUNCATE blocked by FK; surprise identity reset breaks external references that assumed stable ids.
Hostile-panel drills (defend the decision)
Q1. Does TRUNCATE remove columns?
Model answer: No — that myth is wrong. Structure (columns, indexes, constraints) remains; only rows go.
Q2. Rollback TRUNCATE on MySQL InnoDB?
Model answer: No — DDL auto-commits. Treat as irreversible.
Q3. Why is TRUNCATE faster than DELETE FROM t?
Model answer: Page deallocation with minimal logging vs per-row undo/redo and later vacuum/purge.
🤖 Don't fully get this? Learn it with Claude
Stuck on TRUNCATE? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **TRUNCATE** (Databases) and want to truly understand it. Explain TRUNCATE from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **TRUNCATE** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **TRUNCATE** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **TRUNCATE** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.