MySQL
ORACLE
POSTGRESQL
SQL
SQL SERVER

SQL TRUNCATE TABLE: A Complete Guide

intro

Let's learn everything you need to know about the SQL TRUNCATE TABLE statement to empty tables like a pro.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

Table data can grow large, and there may come a point where you need to start fresh by clearing it. This is especially useful in testing or development environments, where data can quickly become irrelevant. So, how do you clean up a table while keeping its structure intact? The answer is the SQL TRUNCATE TABLE statement!

In this guide, you will learn how to truncate a table in SQL and master this operation across the most popular databases.

Let’s dive in!

What Is the SQL TRUNCATE TABLE Statement?

The SQL TRUNCATE TABLE statement removes all rows from a table. Unlike the DELETE statement, TRUNCATE is classified as an SQL DDL (Data Definition Language) command and is more efficient because it drops and re-creates the table.

At the same time, TRUNCATE is not transactional and cannot be rolled back in most cases. This is why it must be used with caution.

How to Truncate a Table in SQL

TRUNCATE is part of the SQL ANSI/ISO standard, so it is available in most databases with the following syntax:

Copy
        
1 TRUNCATE TABLE table_name;

Where table_name is the name of the table you want to empty.

In MySQL and PostgreSQL, the TABLE keyword is optional, so you can also write:

Copy
        
1 TRUNCATE table_name;

Special Notes

You cannot use TRUNCATE TABLE SQL queries on tables that:

  • Are referenced by a FOREIGN KEY constraint.
  • Participate in an indexed view in databases that support them, like SQL Server or constitute a view in any way, shape or form.

Also, keep in mind that TRUNCATE TABLE:

  • Does not activate triggers because it bypasses the DML (Data Manipulation Language) method of deleting data to achieve higher performance. As a result, ON DELETE triggers will not be fired after a TRUNCATE query.
  • Causes an implicit commit, so it cannot be rolled back.

How to Truncate Many Tables in SQL

In PostgreSQL, you can truncate multiple tables in one statement by separating them with commas using this syntax:

Copy
        
1 TRUNCATE TABLE table_name_1, table_name_2, [...], table_name_n;

For other databases, you need to issue separate TRUNCATE statements for each table:

Copy
        
1 TRUNCATE TABLE table_name_1; 2 -- ... 3 TRUNCATE TABLE table_name_n;

Empty a Table in a Visual Database Client

Emptying a table with a SQL TRUNCATE TABLE query is straightforward, but remembering the syntax and unique features across different databases can be challenging. To simplify this process, consider using a visual database client like DbVisualizer.

DbVisualizer lets you easily empty a table through an intuitive UI. These are the steps to get started:

  1. Download DbVisualizer and start a 21-day free Pro Trial.
  2. Set up the tool and connect it to your database.
  3. In the left panel, navigate to the "Tables" dropdown for your database (e.g., ecommerce), and open the dropdown.
  4. Right-click on the table you want to truncate (e.g., categories), then select the "Empty Table..." option.

    Selecting the “Empty Table…” option in DbVisualizer

    Selecting the “Empty Table…” option in DbVisualizer


The following modal window will appear:

Visually truncating a table in DbVisualizer
Visually truncating a table in DbVisualizer

Here, you can choose whether to TRUNCATE or DELETE the table data. As shown in the "SQL Preview" section, DbVisualizer will automatically generate and execute the TRUNCATE query for you.

Wonderful! Emptying tables has never been easier.

Truncate a Table in SQL: Complete Example

Suppose you want to empty the users table with a TRUNCATE TABLE SQL query. First, you may want to verify that it contains some rows with the SQL COUNT() function:

Copy
        
1 SELECT COUNT(*) 2 FROM users;

Execute the query in a visual database client like DbVisualizer, and you will see that users contain 95 rows:

Note the 95 in the Result tabb
Note the 95 in the Result tabb

Then, launch the SQL TRUNCATE TABLE query:

Copy
        
1 TRUNCATE TABLE users;

The result will be:

The table has been truncated as desired
The table has been truncated as desired

Note that for performance reasons, the query does not return the number of deleted rows. The reason behind that is that InnoDB—the primary storage engine used by MySQL—does not store an internal row count, unlike MyISAM, which is now long obsolete.

Once our table is truncated, we can check that the users table is empty by executing the same COUNT() query we used before:

The result tab now shows 0 rows
The result tab now shows 0 rows

Et voilà! The row count is now 0, which means the users table is empty.

TRUNCATE TABLE SQL Best Practices

These are the recommended practices to follow when truncating a table in SQL:

  • Ensure no foreign key constraints: You cannot truncate a table that is referenced by a foreign key constraint.
  • Avoid indexed views: Tables involved in indexed views cannot be truncated.
  • Check SQL Server replication settings: Remember that you cannot truncate tables involved in transactional or merge replication.
  • Consider triggers: TRUNCATE TABLE does not activate triggers, so be cautious if you need to run any triggers associated with data deletion.
  • UseDELETEfor more complex scenarios: If any of the above constraints apply, use the DELETE statement instead of TRUNCATE. If you do that, bear in mind that DELETE will always be slower than TRUNCATE.
  • Data backups: Always ensure you have a backup of your data before truncating a table, as TRUNCATE is irreversible and cannot be rolled back in most cases.

Conclusion

In this guide, you explored the TRUNCATE TABLE SQL statement and learned how to remove data from one or more tables. You also saw how visual tools like DbVisualizer can simplify the process of emptying tables.

DbVisualizer is a powerful database client that streamlines tasks such as query execution, data exploration, and table management. It also offers advanced features like query optimization, SQL formatting, and schema visualization with ERD-like diagrams. Try DbVisualizer for free today!

FAQ

How does table truncation in sql works?

To be fast, table truncation in SQL works by removing all rows without logging individual row deletions. Speficailly, truncate operations drop and re-create the table, which is much faster than deleting rows one by one, particularly for large tables. In detail, SQL Server deallocates the data pages used by the table, effectively freeing up storage in the quickest way possible. Unlike a DELETE statement, truncation does not fire triggers or return the number of rows affected.

What are some special considerations for the Transact-SQL TRUNCATE TABLE statement?

The SQL Server TRUNCATE TABLE statement is similar to the DELETE statement without a WHERE clause. However, TRUNCATE TABLE is faster and uses fewer system and transaction log resources. Additionally, the Transact-SQL TRUNCATE TABLE statement supports truncating specific partitions.

How to truncate table data in SQL Server?

To truncate table data in SQL Server, you must use a TRUNCATE statement with the following syntax:

Copy
        
1 TRUNCATE TABLE 2 { database_name.schema_name.table_name | schema_name.table_name | table_name } 3 [ WITH ( PARTITIONS ( { <partition_number_expression> | <range> } 4 [ , ...n ] ) ) ] 5 [ ; ]

What are some special considerations for the Oracle SQL TRUNCATE TABLE statement?

Some special considerations for the Oracle SQL TRUNCATE TABLE statement include:

  • It cannot be rolled back or restored using FLASHBACK.
  • It deallocates space used by the removed rows but does not affect the MINEXTENTS storage.
  • Foreign key constraints must be disabled before truncating parent tables.
  • It automatically resets indexes and materialized view logs.
  • You can use the CASCADE clause to truncate related child tables with referential integrity constraints.
  • Restrictions apply to partitioned tables, domain indexes, and clustered tables.

What is the difference between DROP TABLE and TRUNCATE TABLE in SQL?

The key difference between DROP TABLE and TRUNCATE TABLE in SQL lies in their effects on the table and schema. DROP TABLE removes the entire table, including its structure, constraints, and data, permanently. It also frees the associated storage space. In contrast, TRUNCATE TABLE only removes the data within the table but retains the table structure. So, DROP removes the table while TRUNCATE only removes its data.

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

MySQL Indexing Mistakes 101

author Lukas Vileikis tags MySQL Search SQL 10 min 2025-03-27
title

A Complete Guide to SQL Window Functions

author Lukas Vileikis tags DATA TYPES DbVisualizer MySQL SQL 5 min 2025-03-25
title

A Complete Guide to the FULL OUTER JOIN SQL Operation

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-03-24
title

How to Use JOIN in a DELETE Query in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-03-20
title

What Is the MySQL DSN Format for Golang?

author Lukas Vileikis tags DbVisualizer MySQL SQL 4 min 2025-03-19
title

Operations Across Entire Columns: SQL Aggregate Functions Explained

author Lukas Vileikis tags DbVisualizer MySQL SQL Transaction 8 min 2025-03-18
title

A Guide to the SQL CREATE TABLE Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-03-17
title

Clustered Indexes in PostgreSQL: What They Are and How to Use Them

author Lukas Vileikis tags DbVisualizer POSTGRESQL SQL 5 min 2025-03-13
title

How to Deal With a Swollen SQL Transaction Log

author Antonello Zanini tags SQL SQL SERVER 7 min 2025-03-12
title

When, How, and Why to Use the Truncate SQL Statement

author Lukas Vileikis tags DbVisualizer MySQL SQL 6 min 2025-03-11

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.