Drop all tables in a MySQL database without dropping the database

Here’s a quick little shell script that will drop all the tables in a MySQL database without dropping the database.

#!/bin/bash

#======================================================================
#  validate command line argument count
#======================================================================
if [ $# -ne 3 ]
then
echo “This program will drop all tables in the specified MySQL database”
echo “Usage: ”
echo “       $0 {user} {password} {database}”
exit 1
fi

#======================================================================
#  Get command line arguments
#======================================================================
user=”$1″
password=”$2″
database=”$3″

#======================================================================
#  Get the list of tables to delete
#======================================================================
tableList=$(mysql -u $user -p$password $database -e ‘show tables’ | awk ‘{ print $1}’ | grep -v ‘^Tables’ )

#======================================================================
#  Iterate over and drop each table
#======================================================================
for table in $tableList
do
echo “Dropping $table table from $database database…”
mysql -u $user -p$password $MDB -e “set FOREIGN_KEY_CHECKS = 0; use $database; drop table $table”
done