Clear Database

Stewart3 years ago

Following the instructions in the https://www.traccar.org/clear-history/ document, I’ve created a file with the following…

#!/bin/bash
while [[ "$result" != *" 0 rows affected"* ]]; do
  result=$(mysql -u root -p[mypassword] traccar -vve "DELETE FROM tc_positions WHERE fixTime < DATE(DATE_ADD(NOW(), INTERVAL -7 DAY)) AND id NOT IN (SELECT positionId FROM tc_devices WHERE positionid IS NOT NULL) LIMIT 10000")
  sleep 1
done
while [[ "$result" != *" 0 rows affected"* ]]; do
  result=$(mysql -u root -p[mypassword] traccar -vve "DELETE FROM tc_events WHERE eventTime < DATE(DATE_ADD(NOW(), INTERVAL -7 DAY)) LIMIT 10000")
  sleep 1
done

However it only clears the history in the tc_positions table and not the tc_events table. The script seems to stop after the first table has been cleared but if I separate the scripts and create 2 files (1 to do tc_positions and another to do tc_events) then they both work.

I don’t know enough about MySQL commands but its there a way to modify the above to make both run in 1 file?

Christoph Krey3 years ago

Just add

result=""

before the second "while"

Stewart3 years ago

Perfect, thanks.