Last active
November 4, 2025 13:53
-
-
Save jcardus/aa6c688ea444f8f274e00a6712e2309c to your computer and use it in GitHub Desktop.
traccar positions cleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| DB="traccar" | |
| USER="neondb_owner" | |
| HOST="ep-late-cherry-a460jx6n.us-east-1.aws.neon.tech" | |
| INTERVAL="2 weeks" | |
| max_id=$(psql -h $HOST -U $USER -d $DB -t -A -c "SELECT max(id) FROM tc_devices;") | |
| cutoff=$(psql -h $HOST -U $USER -d $DB -t -A -c "SELECT NOW() - INTERVAL '$INTERVAL';") | |
| echo "Cutoff time = $cutoff" | |
| for (( device_id=$max_id; device_id>=1; device_id-- )); do | |
| while true; do | |
| sql="SELECT id | |
| FROM tc_positions | |
| WHERE deviceid = $device_id | |
| AND fixtime < '$cutoff' | |
| AND id NOT IN (SELECT positionId FROM tc_devices WHERE positionid IS NOT NULL and id = $device_id) | |
| LIMIT 10000;" | |
| echo "$(date) $sql" | |
| ids=$(psql -h $HOST -U $USER -d $DB -t -A -c "$sql") | |
| if [ -z "$ids" ]; then | |
| break | |
| fi | |
| id_list=$(echo "$ids" | paste -sd "," -) | |
| sql="delete from tc_positions WHERE deviceid=$device_id and id IN ($id_list);" | |
| echo "$(date) ${sql:0:100}..." | |
| echo "$(date) $(psql -h $HOST -U $USER -d $DB -c "$sql")" | |
| done | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment