PostgreSQL – Truncate Table
In PostgreSQL, the TRUNCATE
command is used to remove all data from a table while preserving the structure of the table. TRUNCATE
statement is faster than the DELETE
statement to delete all the rows of a table.
Below is the syntax of the TRUNCATE
command:
TRUNCATE TABLE table_name;
In the above command table_name
is the name of the table that you would like to truncate the data from.
TRUNCATE
command cannot be undone. Once you truncate a table, all data in the table will be permanently deleted.
You can truncate multiple tables at once as shown below by using a comma-separated list of table names:
TRUNCATE TABLE table1, table2, table3;
If the table has any dependent objects, such as indexes, views, or triggers, they will be automatically dropped when you truncate the table.
You can use the CASCADE
option to delete data from the dependent objects along with the primary table.
TRUNCATE TABLE table_name CASCADE;