Skip to content
All writing

NOTES & IDEAS

TRUNCATE or DELETE? Choosing How to Empty a PostgreSQL Table

Removing every row is different from deleting selected records. Here is how locks, triggers, foreign keys, and transactions change the decision.

PostgreSQLDatabasesBackend

A table has 50 million rows. The task is to remove every row while keeping the table ready for future inserts.

My short reply to that question was to use TRUNCATE TABLE. That is a useful starting answer, but the row count is only one part of the decision. The application also cares about what happens while the cleanup runs.

The examples below use an imaginary scratch table, not a database I measured or changed.

Two operations with different jobs

DELETE removes rows matching a condition. Leaving out WHERE removes all rows; adding it lets the application keep some records. PostgreSQL also supports RETURNING when the caller needs information about the deleted rows. See the PostgreSQL DELETE documentation.

For example:

DELETE FROM scratch_events
WHERE processed = true
RETURNING id;

That expresses a row-level decision: remove processed events, keep the others, and return the affected IDs. It is a good fit when the cleanup has a predicate.

TRUNCATE expresses a different intention: empty the entire table. PostgreSQL can do that without scanning each row, which makes it useful for large-table resets. It also reclaims space immediately. The table remains available as a database object afterward. These behaviors are documented in PostgreSQL TRUNCATE.

-- Example for a disposable development table.
BEGIN;
TRUNCATE TABLE scratch_events;
ROLLBACK;

The rollback is intentional. This demonstrates that PostgreSQL can roll back a truncation inside a transaction. It does not demonstrate a performance result.

Ask what must keep working

Imagine two different requirements.

In the first, a disposable import staging table must be emptied before a new dataset is loaded. There is no need to preserve individual rows. A whole-table reset matches that intention.

In the second, an application must remove old customer events while leaving recent events available. That is a different job, even if the table is equally large. A predicate-based deletion better describes what should disappear.

This is why I would describe the operation before describing the optimization. “Remove everything” and “remove the expired subset” are different requirements.

The details behind the faster answer

PostgreSQL's truncation takes an ACCESS EXCLUSIVE lock. It has foreign-key restrictions, and CASCADE can include referencing tables. It fires truncation triggers rather than row-deletion triggers. Sequence values continue by default; resetting owned sequences requires RESTART IDENTITY. These details are covered in the TRUNCATE reference.

For a proposed cleanup, I would write down four questions before selecting the command:

  1. Does every row need to go?
  2. Can other work wait for the required lock?
  3. Does any application behavior depend on deletion triggers?
  4. Which related tables must retain their data?

That small review can change an interview answer into an operational decision. There is no need to invent a timing comparison to explain it.

A more complete answer

For the original whole-table question, TRUNCATE is generally the better starting choice. For selective removal, use DELETE with a clear condition. For either operation, first establish the required behavior around related data and concurrent access.

The lesson I want to retain is to explain why the command matches the task. A faster operation is useful only when it preserves the behavior the application needs.