MySQL Drop All Tables Shell Script Utility

# A shell script to delete / drop all tables from MySQL database. 
# Usage: ./script user password dbnane
# Usage: ./script user password dbnane server-ip
# Usage: ./script user password dbnane
# -------------------------------------------------------------------------
# Copyright (c) 2008 nixCraft project <>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit for more information.
# ----------------------------------------------------------------------
# See URL for more info:
# ---------------------------------------------------
[ "$4" != "" ] && MHOST="$4"
# Detect paths
MYSQL=$(which mysql)
AWK=$(which awk)
GREP=$(which grep)
# help
if [ ! $# -ge 3 ]
	echo "Usage: $0 {MySQL-User-Name} {MySQL-User-Password} {MySQL-Database-Name} [host-name]"
	echo "Drops all tables from a MySQL"
	exit 1
# make sure we can connect to server
$MYSQL -u $MUSER -p$MPASS -h $MHOST -e "use $MDB"  &>/dev/null
if [ $? -ne 0 ]
	echo "Error - Cannot connect to mysql server using given username, password or database does not exits!"
	exit 2
TABLES=$($MYSQL -u $MUSER -p$MPASS -h $MHOST $MDB -e 'show tables' | $AWK '{ print $1}' | $GREP -v '^Tables' )
# make sure tables exits
if [ "$TABLES" == "" ]
	echo "Error - No table found in $MDB database!"
	exit 3
# let us do it
for t in $TABLES
	echo "Deleting $t table from $MDB database..."
	$MYSQL -u $MUSER -p$MPASS -h $MHOST $MDB -e "drop table $t"
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics via:
Category List of Unix and Linux commands
Disk space analyzers df ncdu pydf
File Management cat tree
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities NetHogs dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
8 comments… add one
  • Doug Feb 11, 2017 @ 1:50

    Really great script. I grabbed a few pieces of it for one I’m working on. Saved me a ton of time. I made one tweak to the part where you check if the tables exist.

    # make sure tables exits
    if [ ! $MYSQLTABLES ]
    echo "Error - No table found in $MDB database!"
    exit 3

  • fitorec Aug 31, 2011 @ 13:45

    good script, add internal mysql options:

    TABLES=$($MYSQL -u $MUSER -p$MPASS -h $MHOST $MDB -BNe 'show tables' )
    • – B: Print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file.
    • -N: Do not write column names in results.
  • Matthew Setter Nov 10, 2010 @ 10:16

    thanks for the script, works nicely.


  • jexxer Nov 30, 2009 @ 15:14

    I have one problem with this script.
    It does not take foreign keys into account. It will try to delete tables that have dependencies to other tables and this will throw a error.
    I your script would build a dependencies tree and delete top-down it would be perfect ;)

  • moxxom Jul 6, 2009 @ 6:00

    Good answer , straight but polite and smart

    • antonio May 28, 2011 @ 0:40

      hhahaha very good

  • moxnox Jan 21, 2009 @ 14:59

    Because one might not have the right to create a database.

  • Dallas Marlow Nov 19, 2008 @ 13:11

    why wouldn’t you just drop the database if you were going to drop all of the tables? in mysql even if you drop the db it keeps user and host info, so you could just recreate it.

    drop database $db; create database $db;

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.