Before continuing: This entry is part of the deleting data guide. If you are new to the topic, start with DELETE.
TRUNCATE TABLE example
TRUNCATE TABLE temporary_sessions;Before running a destructive operation, check the object and how many rows it affects:
SELECT COUNT(*) AS current_rows
FROM temporary_sessions;A pre-check turns a vague intention (“clear out the orders”) into a measurable decision. If the number is not what you expected, stop.
What changes from case to case
| Operation | Rows | Structure | Accepts a filter |
|---|---|---|---|
DELETE | Some or all | Kept | Yes, with WHERE |
TRUNCATE TABLE | All | Kept | No |
DROP TABLE | All | Removed | No |
The difference is not just speed. Permissions, triggers, foreign keys, identity counters and transactions can all behave differently.
Do not assume it can always be undone. PostgreSQL and SQL Server let you roll back TRUNCATE inside a transaction under their documented conditions. MySQL treats it as DDL, triggers an implicit commit and does not let you roll it back like a normal DML statement.
Common mix-ups with TRUNCATE TABLE
Using it in place of DELETE with a filter
TRUNCATE TABLE orders WHERE status = 'cancelled' is not valid. If you need to keep some of the rows, use DELETE ... WHERE ... and check the equivalent SELECT first.
It can also fail when other tables reference this one through foreign keys. The exact rules, and options such as CASCADE, depend on the engine.
Key differences between engines
PostgreSQL, MySQL, SQL Server and Oracle include TRUNCATE TABLE, but they do not share all its effects. PostgreSQL can roll it back inside a transaction and offers RESTART IDENTITY and CASCADE; it also takes an exclusive lock. MySQL treats it as DDL, performs an implicit commit and recreates the table. SQL Server resets identity values and allows rollback inside a transaction. SQLite does not implement TRUNCATE TABLE; it uses DELETE FROM table_name, with an internal optimisation when there is no filter.