How to TRUNCATE TABLE in PostgreSQL

Galaxy Glossary

How do I use TRUNCATE TABLE in PostgreSQL to quickly empty a table?

TRUNCATE TABLE instantly removes all rows from one or more PostgreSQL tables, optionally resetting sequences and cascading to related tables.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

What does TRUNCATE TABLE do in PostgreSQL?

TRUNCATE TABLE deletes every row in a table in a single, metadata-level operation. Unlike DELETE, it bypasses row-by-row logging, giving near-instant execution even on millions of records.

How do I quickly empty a table without logging each row?

Use TRUNCATE TABLE when you need a fresh table state and do not require per-row triggers, foreign-key checks, or VACUUM reclamation. It is ideal for nightly ETL staging tables.

What is the basic TRUNCATE TABLE syntax?

Place the TRUNCATE keyword before one or more table names. Add RESTART IDENTITY to reset SERIAL/BIGSERIAL sequences, or CONTINUE IDENTITY to keep them.

Can I truncate multiple tables at once?

Yes. List several tables separated by commas, or use CASCADE to automatically truncate tables that reference them through foreign keys.

How do I reset identity columns after truncation?

Append RESTART IDENTITY so sequences behind SERIAL or IDENTITY columns restart from their start value. Useful for dev and test datasets.

When should I use CASCADE with TRUNCATE?

Use CASCADE only when you are certain that child rows can be lost. For example, truncating Orders might need CASCADE to clear OrderItems.

Best practices for using TRUNCATE TABLE

Wrap TRUNCATE in a transaction so it can be rolled back. Grant the command sparingly; it requires table owner or superuser privileges.

Common pitfalls and how to avoid them

Avoid truncating tables with critical audit data. Remember that triggers do not fire, so downstream logic will not execute.

Why How to TRUNCATE TABLE in PostgreSQL is important

How to TRUNCATE TABLE in PostgreSQL Example Usage


-- Clear daily imports before loading new data
BEGIN;
TRUNCATE TABLE Orders_import, OrderItems_import RESTART IDENTITY;
COMMIT;

How to TRUNCATE TABLE in PostgreSQL Syntax


TRUNCATE [ TABLE ] [ ONLY ] table_name [, ...]
    [ * ]
    [ RESTART IDENTITY | CONTINUE IDENTITY ]
    [ CASCADE | RESTRICT ];

Examples (e-commerce):
-- Empty staging copy of Orders
TRUNCATE TABLE Orders_staging;

-- Empty Orders and OrderItems together, resetting sequences
TRUNCATE TABLE Orders, OrderItems RESTART IDENTITY CASCADE;

Common Mistakes

Frequently Asked Questions (FAQs)

Does TRUNCATE fire DELETE triggers?

No. TRUNCATE bypasses all row-level triggers. If you need trigger logic, use DELETE instead.

Can I roll back a TRUNCATE?

Yes, as long as it occurs inside an open transaction that has not yet been committed.

Is TRUNCATE logged in PostgreSQL?

Minimal logging occurs to support replication and crash recovery, but far less than a DELETE of every row.

Want to learn about other SQL terms?