How to drop all tables in a MySQL/MariaDB database via CLI

Share Button

Every have a database you want to clear out in one shot so you don’t have to get into the MySQL command line interface and drop them one at a time, or phpmyadmin, etc.?  Here’s the command you’ll need:

mysqldump -u user --password='password' --no-data --add-drop-table DBNAME |grep ^DROP | xargs -n 1 -i mysql -u user --password='password' -e "{}" DBNAME

You’ll of course have to replace the user, pass and database name in the above with the relevant values.

If this is being done on a Plesk control panel-based web server, you can use the following command instead, and you only need the database name filled in properly:

mysqldump -u admin --password=`cat /etc/psa/.psa.shadow` --no-data --add-drop-table DBNAME|grep ^DROP | xargs -n 1 -i mysql -u admin --password=`cat /etc/psa/.psa.shadow` -e "{}" DBNAME
Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *