DELETE vs TRUNCATE vs DROP in SQL: Differences Explained
Compare row-by-row logging (DELETE), fast table emptying with auto-increment reset (TRUNCATE), and complete table removal (DROP).
-- DELETE (Slow, logged, where clause):
DELETE FROM logs WHERE created_at < NOW() - INTERVAL '30 days';
-- TRUNCATE (Fast, resets sequence, no where):
TRUNCATE TABLE logs RESTART IDENTITY;
-- DROP (Deletes table schema & data):
DROP TABLE logs CASCADE;Step-by-Step Query Breakdown
DELETE FROM users WHERE status = 'inactive';Deletes specific rows satisfying the WHERE clause. Logs each row deletion in write-ahead logs (WAL), fires triggers, and can be rolled back.
TRUNCATE TABLE users RESTART IDENTITY CASCADE;Instant table wipe by deallocating data pages. Bypasses individual row logging, resets AUTO_INCREMENT / IDENTITY counter, but cannot use WHERE.
DROP TABLE IF EXISTS users CASCADE;Permanently destroys both the data and the entire table schema, indexes, constraints, and permissions.
Critical SQL Pitfalls & Precautions
- β’TRUNCATE cannot be run on tables referenced by foreign keys from other tables unless CASCADE is specified.
- β’In MySQL, TRUNCATE triggers an implicit commit and cannot be rolled back inside a transaction!
DELETE vs TRUNCATE vs DROP - Frequently Asked Questions
Common questions about transactional safety, indexing, and engine compatibility.
Yes! In PostgreSQL, DDL commands including TRUNCATE are fully transactional and can be rolled back within a BEGIN ... ROLLBACK block.
Related SQL Query Guides
Browse All SQL RecipesIdentify rows with duplicate column values using GROUP BY and HAVING count(*) > 1, and safely remove duplicates using ROW_NUMBER() CTEs.
Atomically insert a new row or update an existing row if a unique constraint or primary key conflict is encountered.
Compute cumulative running totals over time or partitioned groups using SUM() OVER (ORDER BY ...).
Build a production database index on a table with millions of rows without locking out live INSERT, UPDATE, or DELETE operations.
Update values in one table based on matching columns or aggregations in a secondary table.
Reset the next auto-generated primary key ID back to 1 or synchronize it with the maximum existing ID in the table.