≡ Menu

MySQL Drop All Tables Shell Script Utility

{ 8 comments… add one }
  • Doug February 11, 2017, 1:50 am

    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 August 31, 2011, 1:45 pm

    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 November 10, 2010, 10:16 am

    thanks for the script, works nicely.


  • jexxer November 30, 2009, 3:14 pm

    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 July 6, 2009, 6:00 am

    Good answer , straight but polite and smart

    • antonio May 28, 2011, 12:40 am

      hhahaha very good

  • moxnox January 21, 2009, 2:59 pm

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

  • Dallas Marlow November 19, 2008, 1:11 pm

    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;

Security: Are you a robot or human?

Leave a Comment