Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. SQL Dictionary
  3. TRUNCATE

TRUNCATE

Since: SQL:2003(2003)

An SQL statement that quickly deletes all rows from a table. It is faster than DELETE and also resets the AUTO_INCREMENT counter.

Syntax

Deletes all rows from the table.

TRUNCATE TABLE table_name;

The TABLE keyword is optional (MySQL).

TRUNCATE table_name;

Differences from DELETE

ItemTRUNCATEDELETE (all rows)
SpeedFast (does not write row-level logs).Slow (logs each row individually).
WHERE clauseNot supported (removes all rows only).Supported.
AUTO_INCREMENTReset to 1.Not reset.
TransactionCannot be rolled back (DDL statement).Can be rolled back.
Rows affectedNot returned (0 rows affected).Returns the number of deleted rows.
Foreign key constraintCannot be used on tables referenced by a foreign key.Supported (raises an error on constraint violation).

Sample Code

The following examples use the temp_import_data table.

temp_import_data id raw_data imported_at 1 user,user_a,300000 2025-03-01 09:00:00 2 user,user_c,280000 2025-03-01 09:00:01 3 user,user_e,320000 2025-03-01 09:00:02 3 rows in set

Clears the table used to store temporary import data.

sample_truncate.sql
TRUNCATE TABLE temp_import_data;
Query OK, 0 rows affected (0.03 sec)
sample_truncate.sql
SELECT * FROM temp_import_data;
Empty set (0.00 sec)

Periodically clears the log table.

sample_truncate.sql
TRUNCATE TABLE access_logs;
Query OK, 0 rows affected (0.02 sec)

Resets the test table (also resets AUTO_INCREMENT to 1).

sample_truncate.sql
TRUNCATE TABLE test_users;
Query OK, 0 rows affected (0.02 sec)

Example of disabling foreign key checks before TRUNCATE (MySQL).

sample_truncate.sql
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE orders;
TRUNCATE TABLE order_items;
SET FOREIGN_KEY_CHECKS = 1;
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.00 sec)

Syntax by Database

TRUNCATE TABLE is supported in MySQL, PostgreSQL, Oracle, and SQL Server.

TRUNCATE TABLE temp_import_data;

In PostgreSQL, TRUNCATE can be rolled back within a transaction (DDL statements are also transaction-safe). You can also add CASCADE to simultaneously delete rows in referencing tables when a foreign key relationship exists.

-- Deletes from the table and all tables that reference it (PostgreSQL).
TRUNCATE TABLE orders CASCADE;

-- Truncates multiple tables at once (PostgreSQL).
TRUNCATE TABLE orders, order_items;

SQLite does not support TRUNCATE TABLE. Use DELETE FROM to remove all rows instead.

-- Delete all rows in SQLite.
DELETE FROM temp_import_data;

Notes

TRUNCATE removes all rows in a manner similar to recreating the table, making it extremely fast even for large tables. It is well suited for periodically resetting log tables or clearing test data.

TRUNCATE is not protected by transactions (BEGIN / ROLLBACK). Once executed, it cannot be undone — always take a backup before running it. It also cannot be used on tables that are referenced by a foreign key in another table.

If you need to delete only specific rows based on a condition, or if you need rollback support, use DELETE instead.

If you find any errors or copyright issues, please .