TRUNCATE(7)

๐Ÿ“ฆ version 18.6, in opensuse-leap 16.0

๐ŸŽ€ 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. if ONLY comes before the table name, then only that table gets truncated. if ONLY is 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 of CASCADE. 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 ๐Ÿฆ‹

DELETE(7)


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