Skip to content

Instantly share code, notes, and snippets.

@leonardobiffi
Created February 23, 2024 16:46
Show Gist options
  • Save leonardobiffi/dd988307f7cb9c6c588e7ba53bd0e513 to your computer and use it in GitHub Desktop.
Save leonardobiffi/dd988307f7cb9c6c588e7ba53bd0e513 to your computer and use it in GitHub Desktop.
MySQL drop all tables in database
#!/bin/bash
USER="$1"
PASS="$2"
DB="$3"
HOST="$4"
# Detect paths
MYSQL=$(which mysql)
AWK=$(which awk)
GREP=$(which grep)
if [ $# -ne 4 ]
then
echo "Usage: $0 {MySQL-Host} {MySQL-User-Name} {MySQL-User-Password} {MySQL-Database-Name}"
echo "Drops all tables from a MySQL Database"
exit 1
fi
TABLES=$($MYSQL -h $HOST-u $USER -p$PASS $DB -e 'show tables' | $AWK '{ print $1}' | $GREP -v '^Tables' )
$MYSQL -h $HOST -u $USER -p$PASS $DB -e "SET FOREIGN_KEY_CHECKS=0;"
for t in $TABLES
do
echo "Deleting $t table from $DB database..."
$MYSQL -h $HOST -u $USER -p$PASS $DB -e "drop table $t"
done
$MYSQL -h $HOST -u $USER -p$PASS $DB -e "SET FOREIGN_KEY_CHECKS=1;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment