TRUNCATE(7)
๐ TRUNCATE(7) ๐
okay bestie, welcome to the TRUNCATE page โ that's manual section 7, part of the PostgreSQL 18.6 Documentation. and babe, this one's a banger.
โจ NAME โจ
TRUNCATE โ empty a table or set of tables
like, poof. gone. no notes. ๐
๐ฆ SYNOPSIS ๐ฆ
TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
๐ DESCRIPTION ๐
so TRUNCATE quickly removes all rows from a set of tables. like, all of them. bye.
it does the exact same thing as an unqualified DELETE on each table, except โ and this is the iconic part โ it doesn't actually scan the tables, which means it's faster. and it's not just faster, it also reclaims disk space immediately, so you don't have to go do a whole VACUUM afterwards. no cap.
where's this most useful? large tables. that's the vibe check. big table, big win.
๐ธ PARAMETERS ๐ธ
nameโ the name (optionally schema-qualified) of a table to truncate. ifONLYcomes before the table name, then only that table gets truncated. ifONLYis NOT there, then the table and all its descendant tables (if any) get truncated too. optionally you can drop a*after the table name to explicitly say "yes, include the descendants." ๐RESTART IDENTITYโ automatically restart sequences owned by columns of the truncated table(s). resetting the vibe, essentially. โจCONTINUE IDENTITYโ do not change the values of sequences. this is the default, bestie, so if you say nothing, this is what you get.CASCADEโ automatically truncate all tables that have foreign-key references to any of the named tables, or to any tables added to the group because ofCASCADE. it ripples. ๐RESTRICTโ refuse to truncate if any of the tables have foreign-key references from tables that aren't listed in the command. this is also the default. she's cautious. she's responsible. we love her. ๐ซถ
๐ฅ NOTES ๐ฅ
you must have the TRUNCATE privilege on a table to truncate it. that's the entry fee, sorry.
TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates on โ and that, bestie, blocks all other concurrent operations on the table. when RESTART IDENTITY is specified, any sequences that are gonna be restarted are likewise locked exclusively. so if you need concurrent access to a table, use the DELETE command instead. DELETE is the chill one. TRUNCATE is the main character who needs the whole room. ๐
TRUNCATE can't 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 to not do one. ๐ค the CASCADE option can auto-include all the dependent tables โ but be very careful when using this option, or else you might lose data you did not intend to! fr fr. and note this specifically: 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. read that twice.
TRUNCATE will not fire any ON DELETE triggers that might exist for the tables. but it will fire ON TRUNCATE triggers. ๐ so 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 the ones listed in the command, and then any that got added due to cascading. queue order, babes. ๐
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 happened. see Section 13.6 for more details. โ ๏ธ
TRUNCATE is transaction-safe with respect to the data in the tables: the truncation will be safely rolled back if the surrounding transaction doesn't commit. so like, she's safe in that way. ๐
when RESTART IDENTITY is specified, the implied ALTER SEQUENCE RESTART operations are also done transactionally โ meaning 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(). that is, 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. it's similar to the usual behavior of currval() after a failed transaction. messy? yes. iconic? also yes. ๐
TRUNCATE can be used for foreign tables if it's supported by the foreign data wrapper โ for instance, see postgres_fdw. ๐ง
โญ EXAMPLES โญ
truncate the tables bigtable and fattable:
TRUNCATE bigtable, fattable;
the same thing, but 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 show up in that standard, but they have slightly different though related meanings. some of the concurrency behavior of this command is left implementation-defined by the standard, so the notes above should be considered and compared with other implementations if necessary. do your homework, bestie. ๐
๐ฆ SEE ALSO ๐ฆ
and that's the whole page, gorgeous. this was TRUNCATE(7) from the PostgreSQL 18.6 docs, dated 2026. go forth and empty tables responsibly โ and remember, CASCADE does not play. ๐โจ๐๐ฆ๐ธ๐
๐ซถ
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms