Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete mysql table data (Bash Script)

Tags:

bash

mysql

I was wondering how to delete the values in a MySQL database table from bash.

I have a database called "database" and a table in that database called "table".

Now I want to dump and clear the database from a bash script.

How can I delete the values?

I am using:

mysqldump database > $DB_BACKUP/01/table-`date +%Y-%m-%d` (to dump)

and now trying to

DELETE FROM database.table WHERE filed < CURDATE()-5
like image 730
tike Avatar asked May 31 '26 22:05

tike


1 Answers

mysql -Bse 'DELETE FROM database.table WHERE filed < CURDATE()- 5'

or

mysql -Bse 'DELETE FROM table WHERE filed < CURDATE()- 5' database


mysql --help

-B, --batch Don't use history file. Disable interactive behavior. starts with this option ENABLED by default! Disable with

-s, --silent Be more silent. Print results with a tab as separator, Buffer for TCP/IP and socket communication

-e, --execute=name Execute command and quit. (Disables --force and history file)

like image 69
Jacek Kaniuk Avatar answered Jun 02 '26 10:06

Jacek Kaniuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!