Skip to the workspace
Pebellslearn by building
Data Definition Language (DDL)  ›  Lesson

TRUNCATE TABLE

Truncate Table 12 minTRUNCATE is a bulk table-emptying statement in databases that support it. Its transaction, trigger, identity and foreign-key behavior depends on the database; SQLite does not implement TRUNCATE.

sql / TRUNCATE TABLE

Explore each scene at your own pace.

01 · The rule

The rule

TRUNCATE TABLE

TRUNCATE is a bulk table-emptying statement in databases that support it. Its transaction, trigger, identity and foreign-key behavior depends on the database; SQLite does not implement TRUNCATE.

Compare bulk removal with DELETE using the actual database's transaction and dependency rules.

A developer has loaded 2 million test orders into a staging database and wants a clean slate before the next test run. DELETE FROM orders; works, but crawls through every row one at a time. TRUNCATE TABLE orders; finishes almost instantly. The difference is in what each one actually does.

The rule

TRUNCATE empties every row but keeps the table itself.

TRUNCATE TABLE orders;

After this, orders still exists with all its columns, types and constraints — it just has zero rows. Compare it with its two neighbours:

Removes rows Can use WHERE Keeps table structure
DELETE FROM orders WHERE … the ones you pick yes yes
TRUNCATE TABLE orders all of them no yes
DROP TABLE orders all of them no no — the table is gone

TRUNCATE is fast because the database releases the table's storage in bulk rather than removing rows one by one. That speed has side effects:

  • No WHERE. It is all rows or nothing.
  • Row-level delete triggers do not fire, because rows are not deleted individually.
  • Auto-numbering may reset. In MySQL, TRUNCATE restarts the auto-increment counter. In PostgreSQL it does so only if you write TRUNCATE TABLE orders RESTART IDENTITY;.
  • Undo depends on the database. PostgreSQL can roll back a TRUNCATE inside a transaction. MySQL commits it immediately, so there is no undo.
  • Referenced tables are protected. If orders has a foreign key pointing at customers, you cannot simply truncate customers; PostgreSQL requires TRUNCATE … CASCADE, which also empties orders.
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.