Sub topic 14
DELETE versus TRUNCATE
Prerequisites
This chapter assumes you can write basic SELECT queries with WHERE, understand tables and rows, and know that a primary key identifies a row. You should also understand that a foreign key connects a child table to a parent table.
Both DELETE and TRUNCATE remove rows. They have different purposes and safety properties.
Use DELETE for targeted removal
DELETE removes rows from a table. A WHERE clause limits which rows are removed.
SELECT id, status
FROM orders
WHERE status = 'cancelled';
DELETE FROM orders
WHERE status = 'cancelled';The SELECT previews the rows that match. The DELETE then removes those same rows. If you omit WHERE, DELETE removes every row from the table:
DELETE FROM orders;The table still exists after either statement. Its columns, constraints, indexes, and other table definitions remain available. A delete can also activate row-level delete triggers, depending on the database system.
Use TRUNCATE for an empty table
TRUNCATE removes all rows from a table as one table-level operation.
TRUNCATE TABLE orders;TRUNCATE does not accept a WHERE clause. It is useful when you intentionally want to empty a staging table, temporary table, or other table completely. It often needs fewer row-by-row operations than DELETE, so it can be faster for a large table.
TRUNCATE keeps the table definition, but its handling of identity or auto-increment values differs by database and options. For example, some systems reset the next generated identifier, while others preserve it. Check your database documentation before relying on either behavior.
| Property | DELETE | TRUNCATE |
|---|---|---|
| Remove selected rows | Yes, with `WHERE` | No |
| Remove every row | Yes, without `WHERE` | Yes |
| Keep the table | Yes | Yes |
| Row-level delete triggers | Usually supported | Often different or not fired |
| Identity counter | Usually preserved | Database-specific |
| Foreign-key restrictions | Depends on references and actions | Often stricter |
Foreign keys and transactions
A foreign key can prevent either operation from removing rows that other tables still reference. For example, if orders.customer_id refers to customers.id, truncating customers may fail while orders exist. Some databases support special options such as cascading truncation, but those options can empty several related tables.
Transaction behavior also differs by database. Some systems allow TRUNCATE to roll back inside a transaction. Others treat it like a DDL operation that commits implicitly. DELETE is generally easier to combine with ordinary transaction control.
Decision rule
Use DELETE ... WHERE when the removal is selective or when you need familiar transaction and trigger behavior. Use TRUNCATE only when the whole table should be emptied and the database-specific consequences are acceptable.
Exercise
Which statement should you use to remove only cancelled orders while keeping shipped orders?
Show answer
Use DELETE FROM orders WHERE status = 'cancelled'; Preview the matching rows with SELECT first.