Skip to content

Instantly share code, notes, and snippets.

@iAugur
Last active April 8, 2022 10:03
Show Gist options
  • Save iAugur/75e804cb9da4a9a79660edf1e4057a8f to your computer and use it in GitHub Desktop.
Save iAugur/75e804cb9da4a9a79660edf1e4057a8f to your computer and use it in GitHub Desktop.
Dropping Tables from a Mysql/Mariadb database

Drop all tables in a Mysql DB

It's not always that easy to drop all of the tables in a db

But here are two great ways:

  1. Use a script to enumerate all of the tables and drop them one by one
  2. Use mysqldump to create a dump with just the schema statements and grep out the drop tables
#!/bin/bash
# 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 mysql.nixcraft.in
# -------------------------------------------------------------------------
# Copyright (c) 2008 nixCraft project <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
# See URL for more info:
# http://www.cyberciti.biz/faq/how-do-i-empty-mysql-database/
# ---------------------------------------------------
MUSER="$1"
MPASS="$2"
MDB="$3"
MHOST="localhost"
[ "$4" != "" ] && MHOST="$4"
# Detect paths
MYSQL=$(which mysql)
AWK=$(which awk)
GREP=$(which grep)
# help
if [ ! $# -ge 3 ]
then
echo "Usage: $0 {MySQL-User-Name} {MySQL-User-Password} {MySQL-Database-Name} [host-name]"
echo "Drops all tables from a MySQL"
exit 1
fi
# make sure we can connect to server
$MYSQL -u $MUSER -p$MPASS -h $MHOST -e "use $MDB" &>/dev/null
if [ $? -ne 0 ]
then
echo "Error - Cannot connect to mysql server using given username, password or database does not exits!"
exit 2
fi
TABLES=$($MYSQL -u $MUSER -p$MPASS -h $MHOST $MDB -e 'show tables' | $AWK '{ print $1}' | $GREP -v '^Tables' )
# make sure tables exits
if [ "$TABLES" == "" ]
then
echo "Error - No table found in $MDB database!"
exit 3
fi
# let us do it
for t in $TABLES
do
echo "Deleting $t table from $MDB database..."
$MYSQL -u $MUSER -p$MPASS -h $MHOST $MDB -e "drop table $t"
done
# Use mysqldump to create a data less dump with drop table commands and strip out all but the drop tables statements
# source: https://tableplus.com/blog/2018/08/mysql-how-to-drop-all-tables.html
# 1) Disable foreign key check
echo "SET FOREIGN_KEY_CHECKS = 0;" > ./temp.sql
# 2) dump the db with no data and drop all tables:
mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' >> ./temp.sql
# 3) Turn the foreign key check back on:
echo "SET FOREIGN_KEY_CHECKS = 1;" >> ./temp.sql
# 4) Restore the db with the dump file:
mysql -u root -p db_name < ./temp.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment