Delete all tables in a MySQL database

By berliner, 18 December, 2019

Quick and dirty on linux based systems.Set the variables:

DB_USER='user'
DB_PASS='password'
DB_NAME='database'
DB_HOST='host'

To preview the commands that will run:

mysql -u$DB_USER -p$DB_PASS -D$DB_NAME -h$DB_HOST -e "show tables" -s   \
  | awk \
    -v DB_USER=$DB_USER \
    -v DB_PASS=$DB_PASS \
    -v DB_NAME=$DB_NAME \
    -v DB_HOST=$DB_HOST \
    '{ print "mysql -u"DB_USER" -p"DB_PASS" -D"DB_NAME" -h"DB_HOST" -e \"DROP TABLE "$1"\""}'

Delete the tables. There is no going back without a backup, so be sure you really want to do this.

mysql -u$DB_USER -p$DB_PASS -D$DB_NAME -h$DB_HOST -e "show tables" -s   \
  | awk \
    -v DB_USER=$DB_USER \
    -v DB_PASS=$DB_PASS \
    -v DB_NAME=$DB_NAME \
    -v DB_HOST=$DB_HOST \
    '{ print "mysql -u"DB_USER" -p"DB_PASS" -D"DB_NAME" -h"DB_HOST" -e \"DROP TABLE "$1"\""}' \
  | sh