I want to truncate database tables using command line.

Any directions will be highly appreciated.

  • Which table and for what purpose? – amitshree Aug 30 '19 at 12:47
  • I created some bash clone scripts for my dev sites and now trying to truncate all tables that are redundant for my dev site. (previous orders, quotes etc.) – Greg Aug 30 '19 at 13:06
  • You should give a look at magento.stackexchange.com/questions/102936/… – amitshree Aug 30 '19 at 13:22
  • @amitshree I know how to truncate the tables in the database but I wanted to automate this in my bash script ;) I have the answer now, thank you for your interest! – Greg Aug 30 '19 at 14:57

TABLES="log_customer log_visitor log_visitor_info log_url log_url_info"

for table in ${TABLES}
  mysql dbname -e "TRUNCATE TABLE ${table};"
  • magic ;) worked like charm – Greg Aug 30 '19 at 14:54

Please try with below steps to do easily..

Step1 : to create one file truncate.php in root, create array with all tables, than array load via foreach with truncateTable().

Step2 : run file via command line php -f truncate.php

also try with this below url :

mysql -Nse 'show tables' db_name | while read table; do mysql -e "truncate table $table" db_name;done

// db_name = database name


hope its work for you

  • hmmm sounds like an option but I have a bash script to clone site and I want to truncate redundant tables (preferably) within the bash script with no additional files :/ – Greg Aug 30 '19 at 13:08
  • :), may be helpful to you check with above url.. – Anas Mansuri Aug 30 '19 at 13:26
  • lol why do you need to truncate all tables??? you love copy-paste? i guess he needs at least core data:) – MagenX Aug 30 '19 at 13:33
  • Yes, I do not need to truncate ALL tables :D Just tables with redundant data :P – Greg Aug 30 '19 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.