Skip to content

Instantly share code, notes, and snippets.

@mehdichaouch
Created November 29, 2022 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehdichaouch/d16c865e19f7f734f7fff706c8282033 to your computer and use it in GitHub Desktop.
Save mehdichaouch/d16c865e19f7f734f7fff706c8282033 to your computer and use it in GitHub Desktop.
Linux Delete/Drop all MySQL tables from the command-line in one command
#!/bin/bash
# /!\ keep space character at begening of command to avoid saving it in history
# real mysql DROP TABLE in one command -- very fast
mysqldump --add-drop-table --no-data -u[USERNAME] -p[PASSWORD] [DATABASE]| grep -e '^DROP \| FOREIGN_KEY_CHECKS'| mysql -u[USERNAME] -p[PASSWORD] [DATABASE]
# OR
# loop x time on DROP TABLE, but no mysqldump -- https://stackoverflow.com/a/38814457/293214
echo "[DATABASE]"| xargs -I{} sh -c "mysql -u[USERNAME] -p[PASSWORD] -Nse 'SHOW TABLES' {}| xargs -I[] mysql -u[USERNAME] -p[PASSWORD] -e 'SET FOREIGN_KEY_CHECKS=0; DROP TABLE []' {}"
# Examples:
mysqldump --add-drop-table --no-data -uroot -proot magento| grep -e '^DROP \| FOREIGN_KEY_CHECKS'| mysql -uroot -proot magento
mysqldump --add-drop-table --no-data -uroot -proot wordpress| grep -e '^DROP \| FOREIGN_KEY_CHECKS'| mysql -uroot -proot wordpress
mysqldump --add-drop-table --no-data -uroot -proot drupal| grep -e '^DROP \| FOREIGN_KEY_CHECKS'| mysql -uroot -proot drupal
mysqldump --add-drop-table --no-data -uroot -proot spip| grep -e '^DROP \| FOREIGN_KEY_CHECKS'| mysql -uroot -proot spip
mysqldump --add-drop-table --no-data -uroot -proot shopware| grep -e '^DROP \| FOREIGN_KEY_CHECKS'| mysql -uroot -proot shopware
mysqldump --add-drop-table --no-data -uroot -proot orocommerce| grep -e '^DROP \| FOREIGN_KEY_CHECKS'| mysql -uroot -proot orocommerce
mysqldump --add-drop-table --no-data -uroot -proot symfony| grep -e '^DROP \| FOREIGN_KEY_CHECKS'| mysql -uroot -proot symfony
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment