SQL

TRUNCATE vs DELETE

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
`DELETE` is logged per-row and respects triggers; `TRUNCATE` deallocates whole pages and resets the table to empty. `TRUNCATE` is much faster on large tables but has caveats (no WHERE, can't be rolled back in some DBs).
SQL
Raw
-- DELETE — row-by-row, logged, slow on huge tables, can be filtered
DELETE FROM logs WHERE created_at < NOW() - INTERVAL '30 days';

-- TRUNCATE — near-instant, resets auto-increment, can\'t be filtered
TRUNCATE TABLE logs;

-- Cascade to dependent tables (PostgreSQL)
TRUNCATE TABLE users RESTART IDENTITY CASCADE;
-- RESTART IDENTITY  → reset SERIAL/IDENTITY columns to start
-- CASCADE           → truncate FK children too (otherwise: error)

-- Differences to know:
--   TRUNCATE             DELETE
--   -----------------    ------------------------------
--   Whole table          WHERE supported
--   No row triggers      Row triggers fire
--   Resets identity      Identity continues
--   Faster on big tables Slower; logged per row
--   Can't roll back      Fully transactional
--   (MySQL InnoDB —      (everywhere)
--    PostgreSQL CAN
--    roll it back)
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.