DELETE and TRUNCATE are both tools for clearing tables but work differently:
Nuances:
ROLLBACK), TRUNCATE does not or depends on the DBMS.Example:
-- Delete orders older than 3 years DELETE FROM orders WHERE created_at < CURRENT_DATE - INTERVAL '3 years'; -- Completely clear the table (fast) TRUNCATE TABLE logs;
Can data be restored after TRUNCATE?
A common mistake: thinking that, like DELETE, TRUNCATE can be rolled back in a transaction. But TRUNCATE usually cannot be rolled back or undone, except in some DBMS (for example, PostgreSQL when working within a transaction).
Example (in most DBMS):
BEGIN; TRUNCATE TABLE orders; ROLLBACK; -- In most DBMS, this will not work, data lost!
Story
Deleting temporary data in a production database with TRUNCATE instead of TEST. The audit trigger for row deletion only fired on DELETE — information about lost data, time, and user is missing. This affected incident resolution.
Story
Clearing a table with TRUNCATE while having a foreign key. SQL threw an error due to referential integrity constraints, the script did not complete, automation stopped. Solution: temporarily remove foreign keys or use DELETE.
Story
Mass deletion of data via DELETE in large tables without batching: due to long locks, the database "froze", other transactions waited for locks to be released. The outcome — system downtime. Correct approach: use DELETE LIMIT/OFFSET if supported, or batch processing.