TRUNCATE(7)
π TRUNCATE(7) β bestie's guide to nuking a table
okay so first thing: this is the TRUNCATE page from the PostgreSQL 16.15 manual, and it's genuinely one of my favourite commands to explain because it's so dramatic. it's giving "delete everything but make it fashion" β¨
πΈ NAME
TRUNCATE β empty a table or a whole set of tables. poof. gone. π¨
π SYNOPSIS
TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
like, that's the whole spell. memorize it bestie π«Ά
β¨ DESCRIPTION
TRUNCATE quickly removes all the rows from a set of tables. it does exactly what an unqualified DELETE on each table would do, but because it doesn't actually scan the tables, it's faster. and not just faster β it also reclaims disk space immediately, instead of making you wait around for a VACUUM afterwards. which is lowkey the whole reason people love it. this is most useful on large tables, where a DELETE would have you sitting there like π§ββοΈ for an hour.
π PARAMETERS
nameβ the name of a table to truncate, optionally schema-qualified. if you putONLYbefore the table name, only that table gets truncated. if you don't putONLY, the table and all its descendant tables (if it has any) get truncated. optionally you can slap a*after the table name to explicitly say "yes, include the descendants" β same vibe, just louder.RESTART IDENTITYβ automatically restart the sequences owned by columns of the truncated table(s). fresh start energy β¨CONTINUE IDENTITYβ don't touch the sequence values at all. this is the default, so if you do nothing, nothing happens to your sequences. iconic behaviour honestly.CASCADEβ automatically truncate all tables that have foreign-key references to any of the named tables, or to any tables that got added to the group because ofCASCADE. it spreads. like gossip. π₯RESTRICTβ refuse to truncate if any of the tables have foreign-key references from tables that aren't listed in the command. this is the default. cautious queen behaviour π
π¦ NOTES
okay this is the part where you actually pay attention, bestie π
you must have the TRUNCATE privilege on a table to truncate it. non-negotiable.
TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates on, which blocks all other concurrent operations on that table. and when RESTART IDENTITY is specified, any sequences that are going to be restarted get locked exclusively too. so if you need concurrent access to a table to keep working, you should use the DELETE command instead. fr fr.
TRUNCATE cannot be used on a table that has foreign-key references from other tables, unless all such tables are also truncated in the same command. why? because checking validity in those cases would require table scans, and the entire point is not to do one. π€ the CASCADE option can be used to automatically include all dependent tables β but be very careful with this option, or else you might lose data you did not intend to! like, actually. note in particular that when the table to be truncated is a partition, sibling partitions are left untouched, but cascading happens to all referencing tables and all their partitions, with no distinction. no thoughts, just cascade. π
TRUNCATE will not fire any ON DELETE triggers that might exist for the tables. but it will fire ON TRUNCATE triggers. if ON TRUNCATE triggers are defined for any of the tables, then all BEFORE TRUNCATE triggers fire before any truncation happens, and all AFTER TRUNCATE triggers fire after the last truncation is performed and any sequences are reset. the triggers fire in the order the tables are to be processed β first those listed in the command, and then any that were added due to cascading. orderly. we love to see it. π
TRUNCATE is not MVCC-safe. after truncation, the table will appear empty to concurrent transactions if they're using a snapshot taken before the truncation occurred. see Section 13.6 for more details, iykyk.
TRUNCATE is transaction-safe with respect to the data in the tables though β the truncation will be safely rolled back if the surrounding transaction doesn't commit. phew π«Ά
when RESTART IDENTITY is specified, the implied ALTER SEQUENCE RESTART operations are also done transactionally; that is, they'll be rolled back if the surrounding transaction doesn't commit. but be aware: if any additional sequence operations are done on the restarted sequences before the transaction rolls back, the effects of those operations on the sequences will be rolled back, but not their effects on currval(). meaning after the transaction, currval() will continue to reflect the last sequence value obtained inside the failed transaction, even though the sequence itself may no longer be consistent with that. this is similar to the usual behavior of currval() after a failed transaction. yeah. it's a whole thing. π
TRUNCATE can also be used for foreign tables if supported by the foreign data wrapper β for instance, see postgres_fdw. slay π
π EXAMPLES
truncate the tables bigtable and fattable:
TRUNCATE bigtable, fattable;
the same, and also reset any associated sequence generators:
TRUNCATE bigtable, fattable RESTART IDENTITY;
truncate the table othertable, and cascade to any tables that reference othertable via foreign-key constraints:
TRUNCATE othertable CASCADE;
π COMPATIBILITY
the SQL:2008 standard includes a TRUNCATE command with the syntax TRUNCATE TABLE tablename. the clauses CONTINUE IDENTITY / RESTART IDENTITY also appear in that standard, but with slightly different though related meanings. some of the concurrency behaviour of this command is left implementation-defined by the standard, so the notes above should be considered and compared with other implementations if necessary. standards are a vibe, but they're not the vibe, y'know? π«
π SEE ALSO
and that's the page!! this absolute icon of a manpage is from PostgreSQL 16.15, dated 2026, and it's filed under TRUNCATE(7) β go forth and truncate responsibly, bestie, and pls don't CASCADE into something you'll regret π
ππ¦
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms