TRUNCATE TABLE
beginnerbasics
2 min read
What is TRUNCATE TABLE?
TRUNCATE TABLE removes all rows from a table in one fast operation. Unlike DELETE, it does not scan every row individually, making it significantly faster for large tables.
The table structure, columns, indexes, and constraints remain intact. Only the data is removed.
Syntax
TRUNCATE TABLE table_name;To truncate multiple tables at once:
TRUNCATE TABLE table1, table2;To also reset identity/serial columns:
TRUNCATE TABLE table_name RESTART IDENTITY;When to Use
- Clearing staging or temporary tables before loading new data
- Resetting a table in a test environment
- Removing all data faster than a
DELETEwithout a WHERE clause
Key Points
- No WHERE Clause — TRUNCATE always removes all rows. Use DELETE if you need to remove a subset.
- Faster Than DELETE — TRUNCATE does not generate individual row-deletion logs, so it is much faster on large tables.
- RESTART IDENTITY — Resets auto-increment (SERIAL/BIGSERIAL) counters back to their initial value.
- CASCADE — TRUNCATE TABLE ... CASCADE also truncates tables with foreign-key references to the target table.
- Not Trigger-Friendly — TRUNCATE fires BEFORE TRUNCATE and AFTER TRUNCATE triggers, not per-row DELETE triggers.
Guided Practice
Solve the challenge below. Use hints when stuck and check your answer for instant feedback.
Practice challengeGuided learning mode
TRUNCATE TABLE Challenge
Write a query that solve this task: remove all rows from the orders table.
Expected result
All rows in orders are removed. The table structure remains.
Hidden checks
- Returned rows and values
- Output columns and result shape
- Final database state after the query runs
Lesson guidance
What is TRUNCATE TABLE?
Initializing database...Each run starts from fresh sample data.
More Examples
Truncate and restart identity
Clear all data and reset the auto-increment counter.
Initializing database...Each run starts from fresh sample data.
Truncate multiple tables with CASCADE
Clear data from enrollments and related referenced tables.
Initializing database...Each run starts from fresh sample data.
Frequently Asked Questions
Is TRUNCATE the same as DELETE without WHERE?
They achieve a similar result, but TRUNCATE is faster because it deallocates data pages instead of deleting rows one-by-one. TRUNCATE also resets identity columns when asked.
Can I TRUNCATE a table with foreign keys?
Only if you use CASCADE. Without CASCADE, PostgreSQL will raise an error if other tables have foreign key references to the truncated table.