-- 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)
Create a free account and build your private vault. Share publicly whenever you want.