Truncate all tables in a MySQL database in one command?

Cover Image for Truncate all tables in a MySQL database in one command?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Truncate All Tables in a MySQL Database with a Single Command 😎

If you've ever wanted to quickly delete all the data in your MySQL database without going through the hassle of dropping and recreating each individual table, you're in luck! There is a simple solution that allows you to truncate all tables in a MySQL database with just one command. 🙌

The Problem 😣

Consider this scenario: You have a MySQL database filled with multiple tables containing huge datasets. You want to start fresh by deleting all the data in the tables, but you don't want to manually truncate each table one by one - definitely not a fun task! 😩

The Solution 💡

Thankfully, MySQL provides a handy command that allows you to truncate all tables in a database in one fell swoop. 👍

Here's the command you can use:

SET FOREIGN_KEY_CHECKS = 0;
SET GROUP_CONCAT_MAX_LEN=32768;
SET @tables = NULL;
SELECT GROUP_CONCAT('`', table_name, '`') INTO @tables
  FROM information_schema.tables
  WHERE table_schema = (SELECT DATABASE());
SELECT IFNULL(@tables,'dummy') INTO @tables;

SET @tables = CONCAT('TRUNCATE TABLE ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET FOREIGN_KEY_CHECKS = 1;

This single command disables foreign key checks, retrieves the names of all the tables in the current MySQL database, dynamically creates a TRUNCATE TABLE command for each table, and executes them to truncate the tables. Finally, foreign key checks are re-enabled to maintain data integrity. 👌

A Step-by-Step Example 🚀

Let's see how this command works in practice using a fictional database called "myDatabase" with two tables: "users" and "orders".

  1. Open your MySQL command-line tool or any MySQL client.

  2. Select the database you want to truncate. In this case, it would be "myDatabase".

    USE myDatabase;
  3. Copy and paste the above command that performs the truncation operation.

  4. Execute the command.

That's it! All the tables in your database will be truncated, which means all the data will be deleted while keeping the structure intact. 😎

Conclusion 🎉

Truncating all tables in a MySQL database in one command can save you a lot of time and effort. Whether you're starting fresh, testing, or simply cleaning up your database, this trick will come in handy. Just remember to use it with caution as it permanently deletes all data!

So, the next time you find yourself needing to delete all the data from your MySQL database, give this command a try. Your future self will thank you! 😉💪

Have you tried this command before? Share your experiences or any other handy database tips in the comments below. Let's discuss! 👇👇👇


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello