Skip to content

Instantly share code, notes, and snippets.

@lavoiesl
Last active August 29, 2015 14:08
Show Gist options
  • Save lavoiesl/90ff27e94a1fd3338112 to your computer and use it in GitHub Desktop.
Save lavoiesl/90ff27e94a1fd3338112 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Drop all tables of a MySQL database
# https://gist.github.com/lavoiesl/96aa8586b609daac2dad
if [ -z "$@" ]; then
echo "Usage: $0 database_name" >&2
echo " or : $0 -uuser -ppassword -hlocalhost database_name" >&2
exit 1
fi
tables=$(mysql $@ -NB -e "SHOW TABLES;" | sed -E 's/^(.+)$/\`\1\`/')
if [ -z "${tables}" ]; then
echo "Database is empty"
exit
fi
joined=$(echo "${tables}" | tr "\n" ", " | sed 's/,$//')
echo "DROP TABLE $joined;"
echo
echo -n "Confirm (y/N) ? "
read confirm
if [ "${confirm}" = "y" ]; then
mysql $@ -e "SET foreign_key_checks = 0; DROP TABLE $joined; SET foreign_key_checks = 1;"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment