TRUNCATE(7)
π TRUNCATE(7) β the PostgreSQL 15.19 glow-up β¨
okay so bestie, we're talking about TRUNCATE today β it's in the PostgreSQL 15.19 documentation and it's genuinely one of my fave commands because it just deletes everything with zero drama. π
β¨ NAME β¨
TRUNCATE β empty a table or a whole little squad of tables π«Ά
πΈ SYNOPSIS πΈ
TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
π¦ DESCRIPTION π¦
So TRUNCATE quickly removes all the rows from a set of tables. Like, all of them. Iconic behavior honestly.
It does the exact same thing as an unqualified DELETE on each table, but here's the slay: since it doesn't actually scan the tables, it's faster. And it reclaims disk space immediately β no waiting around for a later VACUUM operation like some kind of peasant. This is genuinely most useful on large tables, where DELETE would be taking forever and you'd be sitting there like π.
π§ PARAMETERS π§
nameβ the name of a table to truncate, optionally schema-qualified. If you putONLYbefore the table name, then only that table gets truncated. If you don't specifyONLY, the table and all its descendant tables (if any exist) get truncated too. And optionally you can slap a*after the table name to explicitly say "yes bestie, include the descendants."RESTART IDENTITYβ automatically restarts sequences owned by columns of the truncated table(s). Fresh start energy π₯CONTINUE IDENTITYβ do not change the values of sequences. This is the default, no cap.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. (spooky, see notes below π)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, and it's the responsible main character choice.
π NOTES π
You must have the TRUNCATE privilege on a table to truncate it. No privilege, no truncation, periodt.
TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates on, which β and this is the important part β blocks all other concurrent operations on the table. When RESTART IDENTITY is specified, any sequences that are to be restarted are likewise locked exclusively. So if you need concurrent access to a table, 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 when using this option, or else you might lose data you did not intend to! π Note in particular that when the table to be truncated is a partition, sibling partitions are left untouched, but cascading occurs to all referencing tables and all their partitions with no distinction. Like, none. Zero. π
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 are fired before any truncation happens, and all AFTER TRUNCATE triggers are fired after the last truncation is performed and any sequences are reset. The triggers fire in the order that the tables are to be processed β first those listed in the command, and then any that were added due to cascading. Order matters, bestie. π
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. π
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. 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 these operations on the sequences will be rolled back, but not their effects on currval(). So 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. It's giving "it is what it is." β
TRUNCATE can be used for foreign tables if supported by the foreign data wrapper β for instance, see postgres_fdw. Werk. π
π 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 have slightly different though related meanings β so like, don't assume it's a 1:1 vibe match. Some of the concurrency behavior of this command is left implementation-defined by the standard, so the above notes should be considered and compared with other implementations if necessary. iykyk π«
π SEE ALSO π
DELETE(7) β the other way to get rid of stuff, but slower, and honestly a different vibe entirely.
okay that's the whole page, bestie!! this was TRUNCATE(7) from the PostgreSQL 15.19 docs, dated 2026 β go forth and empty those tables responsibly, and remember: CASCADE is not a toy. π
β¨π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql15 15.19-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