MySQL
ORACLE
POSTGRESQL
SQL
SQL SERVER

How to Truncate Many Tables In SQL at Once

intro

Let’s learn everything you need to know about the truncate many tables SQL approach to emptying multiple tables at the same time.

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

If you ever worked with a database, you are probably familiar with the TRUNCATE operation to empty a table. Since data decluttering is often ongoing, you might want to clean multiple tables at once. So, is there a truncate many tables SQL operation? Which databases support it, and how can you execute it?

In this guide, you will find answers to these questions and learn how to truncate multiple tables in SQL. Let’s dive in!

What is Truncating?

Truncating refers to the process of removing all data in a given table at once. It’s different from a DELETE statement in the following ways:

  1. The DELETE statement can be used to get rid of one row or a set of rows, while TRUNCATE removes all rows from a table in one go.
  2. DELETE accepts parameters after itself. TRUNCATE does not.
  3. The database will not implicitly COMMIT a transaction after running a DELETE operation. The same cannot be said about a TRUNCATE.

A TRUNCATE query will always be faster than a DELETE query because TRUNCATE doesn’t come with the same overhead as a DELETE — if you need to delete all rows within a table at once, ditch DELETE and use TRUNCATE instead.

Why Truncate Multiple Tables in SQL Simultaneously?

Truncating multiple tables in SQL simultaneously can come in handy in many scenarios and for a variety of reasons, including:

  • Eliminating the need to execute multiple TRUNCATE statements individually and thus reducing the overhead of multiple round trips to the database.
  • In cases where you need to clear data from several tables at once, such as during data resets or testing setups.
  • To maintain consistency. For example, if you need to clear data from related tables, doing it in one operation ensures that all affected tables are truncated together, avoiding potential data integrity issues.
  • To reduce the risk of human error, as you only need to run one command instead of many.

Time to discover how to perform this useful operation in SQL!

Truncating Multiple Tables in SQL: A Step-by-Step Guide

Even though the TRUNCATE statement is part of the ANSI/ISO SQL standard, popular databases provide different implementations. MySQL, SQL Server, and Oracle have a similar syntax for it, while PostgreSQL extends it with special features.

In this section, you will see different truncate many tables SQL approaches—including using a database client like DbVisualizer.

MySQL, SQL Server, and Oracle

MySQL, SQL Server, and Oracle do not support an explicit syntax to truncate multiple tables in a single query. Instead, you need to run multiple TRUNCATE statements individually, as shown below:

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

You may be tempted to wrap the above query in a single transaction, but keep in mind that TRUNCATE statements cause an implicit commit in MySQL and Oracle. So, that would not make sense or even be allowed. Only SQL Server supports rolling back TRUNCATE TABLE operations.

PostgreSQL

In PostgreSQL, the TRUNCATE TABLE operation supports truncating multiple tables with a single query with the following syntax:

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

This allows you to efficiently perform a truncate many tables SQL operation.

With a Database Client

Truncating tables via SQL queries is a powerful operation, but what if you prefer a visual approach? With a full-featured, multi-database visual client like DbVisualizer, you can perform this operation effortlessly!

To get started, download DbVisualizer for free, install it, and set up a connection to your database. Then, in the left-hand column, navigate to your tables:

Navigating to the database tables in DbVisualizer
Navigating to the database tables in DbVisualizer

Select the tables you want to truncate by holding Ctrl (or Command on macOS), right-click, and select the "Empty Table…" option:

Selecting the desired tables to truncate
Selecting the desired tables to truncate

Next, a modal window will appear where you can customize the truncation behavior:

The multiple table truncation modal
The multiple table truncation modal

When ready, click "Execute" to truncate the tables, or copy the generated SQL query to execute it manually.

Well done! Truncating multiple tables has never been easier with DbVisualizer.

Note: This feature is available only in the Pro version of DbVisualizer. Take advantage of the 21-day free trial to explore all the features it has to offer!

Truncate Many Tables SQL Best Practices

Below is a list of best practices to truncate many tables in SQL like a pro:

  • Ensure the tables you want to truncate do not have foreign key constraints unless the database you are using supports cascading truncation and you want to perform that.
  • If supported by your database, wrap multiple truncation operations in a transaction to maintain consistency in case of an error. Keep in mind that MySQL and Oracle do not support this option.
  • In PostgreSQL, use the TRUNCATE TABLE table_1, ..., table_n; syntax to truncate multiple tables efficiently in one query.
  • Always create a backup of your data in production before truncating, as the operation is not reversible. If you are a MySQL user, read our guide on mysqldump. For PostgreSQL, see our article on pg_dump.
  • Double-check table names to make sure you are not truncating critical or system tables inadvertently.
  • Keep a record of which tables were truncated and why, especially for documentation, to avoid confusion among your team.

Conclusion

In this guide, you saw why you may need to truncate multiple tables at once in SQL and how to do it in MySQL, SQL Server, and PostgreSQL. You also discovered that only PostgreSQL truly supports truncating multiple tables in a single query.

As shown here, truncating multiple tables becomes much easier with a visual database client like DbVisualizer. This tool greatly simplifies query execution, data exploration, and table discovery, while also offering advanced features like query optimization, SQL formatting, and ERD-like schema generation. Try DbVisualizer for free today!

FAQ

What is the truncate many tables SQL query syntax?

PostgreSQL is the only major database that supports a special syntax for truncating multiple tables at once. Find the syntax below:

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

This allows truncating multiple tables in a single operation.

Is it possible to truncate multiple tables at once in SQL?

Yes, some databases allow you to truncate multiple tables at once in SQ. In PostgreSQL, you can use the TRUNCATE TABLE statement with a comma-separated list of table names to truncate multiple tables in a single query. However, most other major databases, including MySQL, SQL Server, and Oracle, do not support this feature and require individual TRUNCATE statements for each table.

How can you truncate all tables in a database?

To truncate all tables in a database, you need to truncate each table individually. What you need to do is loop through the tables and execute TRUNCATE for each one. You can find the names of the tables in a database in the system tables.

Why should you use a database client to truncate multiple tables in SQL?

Using a database client to truncate multiple tables in SQL offers several advantages:

  • Simplifies the process by giving you the ability to select and execute the TRUNCATE command without writing individual queries for each table.
  • Reduces the risk of errors, such as truncating the wrong tables by providing visual feedback.
  • Allows you to batch operations, improving efficiency and effectiveness.

What are some considerations for truncating multiple tables in SQL?

When performing a truncate many tables SQL query, consider the following:

  • Ensure there are no foreign key constraints preventing truncation or use the CASCADE option if supported.
  • Do not forget that truncating is irreversible, so back up your data beforehand.
  • Truncating bypasses transactional logging in most databases, making rollback difficult or even impossible.
  • Verify that you have the necessary privileges to truncate tables.
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

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
title

SQL PIVOT: How to Use a Pivot Table In SQL

author Leslie S. Gyamfi tags MySQL POSTGRESQL SQL SQL SERVER 9 min 2025-03-10
title

MySQL ALTER TABLE Statement: Definitive Guide

author Antonello Zanini tags ALTER TABLE MySQL 11 min 2025-03-06
title

A Complete Guide to the SQL CREATE INDEX Statement

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

A Complete Guide to the Order of Execution in SQL

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

MySQL NOW Function: Get the Current Date and Time

author Antonello Zanini tags DATETIME MySQL 5 min 2025-02-27
title

A Complete Guide to the MySQL CTE Mechanism

author Antonello Zanini tags MySQL 10 min 2025-02-26
title

SQL Operators: A Complete Walkthrough

author Lukas Vileikis tags DbVisualizer MySQL SQL 4 min 2025-02-25
title

PostgreSQL Truncate Table Reset Sequence Guide

author Antonello Zanini tags POSTGRESQL 6 min 2025-02-17

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.