0

I want to truncate database tables using command line.

Any directions will be highly appreciated.

Greg
  • 2,929
  • 4
  • 39
  • 84
  • 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 https://magento.stackexchange.com/questions/102936/magento-2-how-to-truncate-customers-products-reviews-and-orders-table – 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

2 Answers2

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
MagenX
  • 3,820
  • 1
  • 16
  • 30
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

Anas Mansuri
  • 2,627
  • 1
  • 11
  • 28
  • 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