TRUNCATE TABLE

Also known as: TRUNCATE

On this page 7

TRUNCATE TABLE is an SQL statement that removes every row from a table in a single operation while keeping the table definition, columns, indexes, constraints and permissions intact. In SQL, it is the fastest standard way to empty a table, because the database engine deallocates whole data pages instead of deleting and logging each row individually.

The statement is often shortened to TRUNCATE and was added to the SQL standard in SQL:2008, although products such as Oracle Database, Microsoft SQL Server, MySQL and PostgreSQL supported it long before. Many systems classify it as DDL rather than DML, which explains much of its distinctive behavior.

How TRUNCATE TABLE empties a table

Instead of scanning rows, TRUNCATE TABLE releases the storage that holds the table data and resets the table to an empty state. In SQL Server, the engine deallocates data pages and extents and logs only those deallocations; in PostgreSQL, it creates a new empty data file for the table and its indexes and removes the old files at commit; in MySQL with InnoDB, it is implemented as a drop and re-create of the table.

Because the work depends on the number of allocated pages rather than the number of rows, truncating a table with one billion rows typically takes about as long as truncating one with a thousand rows. The basic syntax is TRUNCATE TABLE table_name;, and several systems accept optional clauses.

The table below lists the most common options and their effect.

OptionSupported byEffect
RESTART IDENTITYPostgreSQL, SQL standardResets identity and owned sequences to their start value
CONTINUE IDENTITYPostgreSQL, SQL standardKeeps current sequence values (default in PostgreSQL)
CASCADEPostgreSQL, Oracle (12c and later)Also truncates tables that reference the target through foreign keys
WITH (PARTITIONS ...)SQL Server 2016 and laterTruncates only the listed partitions
REUSE STORAGEOracle DatabaseKeeps allocated extents for later inserts
Frequently used TRUNCATE TABLE options

TRUNCATE TABLE compared with DELETE and DROP TABLE

The three statements remove data at different levels: rows, contents or the whole object. DELETE without a WHERE clause removes rows one by one and fires row-level triggers; TRUNCATE TABLE removes all contents at once; DROP TABLE removes the table definition together with its data.

The following comparison shows how the statements differ in behavior that matters in production.

PropertyDELETETRUNCATE TABLEDROP TABLE
Statement categoryDMLDDL in most systemsDDL
WHERE clauseSupportedNot supportedNot applicable
LoggingEvery row loggedPage deallocation loggedObject removal logged
Row-level DELETE triggersFiredNot firedNot fired
Identity counterKeptUsually resetRemoved with the table
Table structure afterwardKeptKeptRemoved
Speed on large tablesSlow, proportional to rowsFast, nearly constantFast
Row removal statements compared

A practical consequence is that DELETE can be filtered and audited row by row, whereas TRUNCATE TABLE is an all-or-nothing operation intended for staging, scratch and reloadable tables.

Transactions, rollback and locking

Whether TRUNCATE TABLE can be rolled back depends on the database product. A widespread misconception claims that it can never be undone, which is true only in systems that issue an implicit commit for DDL statements.

Database systemRollback possibleNotes
PostgreSQLYesFully transactional, but not MVCC-safe for concurrent snapshots
Microsoft SQL ServerYesRollback works inside an explicit transaction
Oracle DatabaseNoImplicit commit before and after the statement
MySQLNoCauses an implicit commit
SQLiteNot applicableNo TRUNCATE; DELETE without WHERE uses a truncate optimization
Transactional behavior by product

TRUNCATE TABLE also requires a strong exclusive table lock: an ACCESS EXCLUSIVE lock in PostgreSQL and a schema modification lock in SQL Server. While the statement waits for or holds that lock, all reads and writes on the table are blocked, so a long-running query on the table can stall the truncate and every session queued behind it.

Restrictions and required permissions

Most systems refuse to truncate a table referenced by a foreign key from another table, even when the referencing table is empty, because the engine does not check individual rows. The usual remedies are to truncate the child and parent together, use CASCADE where supported, or temporarily drop the constraint.

  • In SQL Server, tables that participate in indexed views, transactional replication or change data capture cannot be truncated.
  • The required privilege is often broader than for DELETE: SQL Server requires ALTER permission on the table, while PostgreSQL has a dedicated TRUNCATE privilege.
  • Statement-level ON TRUNCATE triggers exist in PostgreSQL, but row-level DELETE triggers never fire, so audit logic built on them is silently bypassed.
  • Logical replication and change capture tools may handle truncation differently from deletes, so downstream replicas must be checked.

When to use TRUNCATE TABLE

TRUNCATE TABLE is the right choice when all rows must go and no per-row logic is needed. The decision table below summarizes typical situations.

SituationRecommended statementReason
Reloading a staging table in an ETL jobTRUNCATE TABLEFast, minimal logging, resets storage
Removing rows older than 90 daysDELETE with WHEREOnly a subset must be removed
Clearing an audited business tableDELETETriggers and audit records must fire
Retiring a table permanentlyDROP TABLEThe structure is no longer needed
Emptying one month of a partitioned tablePartition truncate or dropAffects one partition only
Choosing a statement for removing data

Because the statement is fast and irreversible in several products, teams commonly restrict the privilege to deployment and ETL accounts and require a verified backup before running it on any table that holds business data.

The idea in brief

Imagine a filing cabinet full of folders. DELETE takes out the folders one at a time and writes down each one it removes. TRUNCATE TABLE swaps the drawer for an empty one, keeping the cabinet and its labels exactly as they were.

That makes it very fast for emptying large tables. The catch is that it removes everything, skips per-row checks and, in some databases, cannot be undone once it runs.

A practical scenario

A retail analytics team loads about 25 million sales rows every night into a staging table before transforming them into reporting tables. The original job began with DELETE FROM stage_sales;, which took roughly 40 minutes, generated tens of gigabytes of transaction log and sometimes filled the log disk.

After switching to TRUNCATE TABLE stage_sales;, the clearing step finished in under two seconds and the log growth practically disappeared. The team also moved the statement to a dedicated ETL account, because the job had previously run with broad administrator rights.

One lesson came during testing on SQL Server: the truncate failed because a reporting table had a foreign key pointing to the staging table. The team removed that unnecessary constraint, and the nightly load now completes about 45 minutes earlier.