DELETE & TRUNCATE: Concept
Module: Data Modification & Transactions
DELETE and TRUNCATE both remove data from tables, but they work completely differently under the hood. DELETE removes specific rows one-by-one based on a WHERE clause - it checks each row, fires triggers, logs every deletion, and supports rollback. Think of DELETE as carefully removing items from a shelf one at a time. TRUNCATE removes all rows by deallocating the entire data pages - it does not check rows, does not fire triggers, uses minimal logging, and resets identity columns. Think of TRUNCATE as throwing away the entire shelf. The performance difference is massive: DELETE processes 10,000 rows/second, TRUNCATE processes 1M+ rows/second (100x-1000x faster). But TRUNCATE has restrictions: no WHERE clause (removes all rows), cannot be used with foreign key references (in most databases), and rollback behavior varies by database. Understanding when to use each operation is critical for production systems.
DELETE Statement Deep Dive:
DELETE removes rows from a table based on a WHERE clause. The database engine processes DELETE row-by-row: (1) Scans table to find matching rows, (2) Checks each row against WHERE condition, (3) Fires BEFORE DELETE triggers, (4) Removes row from table, (5) Logs deletion in transaction log, (6) Updates indexes, (7) Fires AFTER DELETE triggers, (8) Checks foreign key constraints. This row-by-row processing makes DELETE slow but flexible.
DELETE Syntax:
DELETE FROM table_name WHERE condition;
Example: DELETE FROM orders WHERE order_date < '2023-01-01';
This deletes all orders before 2023. The database: (1) Scans orders table, (2) Checks order_date for each row, (3) Deletes matching rows one-by-one, (4) Logs each deletion, (5) Updates indexes on orders table. For 1 million old orders, this takes 100 seconds (10,000 rows/sec).
DELETE without WHERE is extremely dangerous:
DELETE FROM orders; -- Deletes ALL orders (millions of rows)
This is the most common production disaster. Always use WHERE clause. Many companies enforce this with database triggers or code reviews.
DELETE Performance Characteristics:
- Speed: 10,000-50,000 rows/second (depends on indexes, triggers, logging)
- Logging: Fully logged (every row deletion recorded in transaction log)
- Triggers: All DELETE triggers fire (can slow down significantly)
- Indexes: Updated for each deletion (overhead for each row)
- Rollback: Fully supported (can undo with ROLLBACK)
- Identity: Does NOT reset (next insert continues from last value)
- Locks: Row-level or page-level locks (can block other queries)
When to use DELETE:
- Remove specific rows (WHERE clause needed)
- Need to fire triggers (audit logging, cascade updates)
- Need rollback support (might need to undo)
- Table has foreign key references (TRUNCATE not allowed)
- Selective cleanup (delete old data, remove duplicates)
TRUNCATE Statement Deep Dive:
TRUNCATE removes all rows from a table by deallocating data pages. Instead of deleting rows one-by-one, TRUNCATE tells the database to release the entire storage space used by the table. Think of it as formatting a hard drive instead of deleting files one-by-one. This makes TRUNCATE 100x-1000x faster than DELETE.
TRUNCATE Syntax:
TRUNCATE TABLE table_name;
Example: TRUNCATE TABLE temp_orders;
This removes all rows from temp_orders instantly. The database: (1) Deallocates all data pages, (2) Resets identity/sequence to starting value, (3) Minimal logging (only page deallocations), (4) No triggers fire, (5) No row-by-row processing. For 1 million rows, this takes 0.1 seconds (1M+ rows/sec).
TRUNCATE Performance Characteristics:
- Speed: 1M+ rows/second (100x-1000x faster than DELETE)
- Logging: Minimal logging (only page deallocations, not individual rows)
- Triggers: No triggers fire (cannot use for audit logging)
- Indexes: Remain intact (structure preserved, data removed)
- Rollback: Varies by database (PostgreSQL: yes, MySQL: no, SQL Server: yes if in transaction)
- Identity: Resets to starting value (next insert starts from 1)
- Locks: Table-level lock (blocks all access during operation)
TRUNCATE Restrictions:
- No WHERE clause (removes all rows, cannot be selective)