Sub topic 13
DROP, TRUNCATE and DELETE Compared
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, and understand primary keys and foreign keys. This chapter uses PostgreSQL syntax where behavior differs between database systems.
The three statements remove data at different levels:
DELETEremoves rows from a table. The table remains.TRUNCATEremoves all rows quickly. The table remains, but filtering is not supported.DROP TABLEremoves the table itself, including its definition, indexes, and table-level constraints.
Compare the effects
| Statement | Removes rows | Removes table definition | Supports `WHERE` | Typical use |
|---|---|---|---|---|
| `DELETE` | Selected or all | No | Yes | Remove matching records |
| `TRUNCATE` | All | No | No | Empty a table quickly |
| `DROP TABLE` | All | Yes | No | Retire the table permanently |
DELETE is a data manipulation language (DML) statement. It processes rows and can use a WHERE clause. Without WHERE, it removes every row, so check the command carefully.
DELETE FROM audit_events
WHERE created_at < DATE '2024-01-01';The database can enforce row-level foreign key rules while deleting. A delete may also activate delete triggers, depending on the database and trigger definition.
Empty a table with TRUNCATE
TRUNCATE removes every row without evaluating a filter for each row. It is often faster than an unfiltered DELETE, especially for a large table. It does not remove the table's columns, indexes, or constraints.
TRUNCATE TABLE staging_orders;In PostgreSQL, you can also remove dependent rows and reset identity sequences:
TRUNCATE TABLE orders
RESTART IDENTITY
CASCADE;CASCADE here can truncate tables that reference orders. Use it only when removing those related rows is intended. Other database systems have different options and trigger behavior. Check the target system before using this statement in a migration.
Remove the table with DROP TABLE
DROP TABLE removes the schema object, not just its contents.
DROP TABLE IF EXISTS staging_orders;After this statement, queries against staging_orders fail until another CREATE TABLE statement creates it. A table referenced by a foreign key or used by a view may prevent the drop. PostgreSQL lets you choose the dependency behavior explicitly:
DROP TABLE staging_orders RESTRICT;
DROP TABLE staging_orders CASCADE;RESTRICT refuses the operation when dependent objects exist. CASCADE removes dependent objects, such as views and foreign key constraints. It can cause a much wider change than the single table named in the statement.
Transactions and safety
Some databases allow DELETE, TRUNCATE, and DROP TABLE to roll back inside a transaction. PostgreSQL supports transactional versions of these operations. MySQL commonly treats TRUNCATE and many other DDL statements as implicit commits, so a rollback may not restore the data or table.
BEGIN;
TRUNCATE TABLE staging_orders;
-- Inspect the result before making it permanent.
ROLLBACK;Use DELETE when you need a condition or row-level processing. Use TRUNCATE when the whole table should be emptied but its structure must remain. Use DROP TABLE only when the table is no longer needed, after checking backups, dependencies, and application code.
Exercise
Which statement should you choose to remove all rows while keeping the table available for future inserts?
Show answer
Use TRUNCATE. Use DELETE instead when you need a WHERE clause or database-specific row-level delete behavior.