0

I want to truncate database tables using command line.

Any directions will be highly appreciated.

4
  • Which table and for what purpose?
    – amitshree
    Commented Aug 30, 2019 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
    Commented Aug 30, 2019 at 13:06
  • You should give a look at magento.stackexchange.com/questions/102936/…
    – amitshree
    Commented Aug 30, 2019 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
    Commented Aug 30, 2019 at 14:57

2 Answers 2

2
#!/bin/bash

TABLES="log_customer log_visitor log_visitor_info log_url log_url_info"

for table in ${TABLES}
do
  mysql dbname -e "TRUNCATE TABLE ${table};"
done
1
  • magic ;) worked like charm
    – Greg
    Commented Aug 30, 2019 at 14:54
1

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

https://tableplus.com/blog/2018/08/mysql-how-to-truncate-all-tables.html

hope its work for you

4
  • 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
    Commented Aug 30, 2019 at 13:08
  • :), may be helpful to you check with above url.. Commented Aug 30, 2019 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
    Commented Aug 30, 2019 at 13:33
  • Yes, I do not need to truncate ALL tables :D Just tables with redundant data :P
    – Greg
    Commented Aug 30, 2019 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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