Before continuing: This entry is part of the deleting data guide. If you are new to the topic, start with DELETE.
Examples
| Statement | Accepts a filter? | Keeps the table? | Typical use |
|---|---|---|---|
DELETE | Yes, with WHERE | Yes | Delete specific rows, or all rows, following the engine's normal delete logic. |
TRUNCATE TABLE | No | Yes | Empty a whole table when the engine and dependencies allow it. |
DROP TABLE | No | No | Remove the table, its data and the objects tied to its definition. |
DELETE FROM orders WHERE status = 'cancelled';
-- Both operations affect the entire table:
TRUNCATE TABLE orders;
DROP TABLE orders;Differences and trade-offs
Safe routine before deleting
- Write a
SELECTwith the same condition and review the rows. - Check the foreign keys, views, processes and permissions that depend on the table.
- Decide whether you need to keep the structure, the unselected data and the identifiers.
- Use a transaction when the engine and the specific statement allow it to be rolled back.
- Check the result before committing, and keep a separate recovery plan.
SELECT id, status, total
FROM orders
WHERE status = 'cancelled';
BEGIN;
DELETE FROM orders
WHERE status = 'cancelled';
SELECT COUNT(*) AS remaining_cancelled
FROM orders
WHERE status = 'cancelled';
ROLLBACK;The example ends with ROLLBACK so you can practise without keeping the delete. In production, check the transaction syntax and restrictions of your engine.
Differences you should not generalise
How TRUNCATE and data definition statements behave varies between engines. Rollback support, identity column handling, trigger execution, permissions and foreign key handling can all differ.
- PostgreSQL: documents
TRUNCATEas an operation that removes every row without scanning them one by one and that can take part in a transaction. - SQL Server: keeps the structure, normally resets the identity counter and can be rolled back inside a transaction, with documented limitations.
- MySQL: treats
TRUNCATE TABLEas a data definition statement with its own rules; do not assume it behaves like a transactionalDELETE. - SQLite: has no
TRUNCATE TABLE; aDELETEwithout a filter can use an internal optimisation.