Course content
How to Purge (Delete) Old Data in Traccar
This article will guide you through manual and automatic methods to purge old data in Traccar, ensuring an optimized and efficient system.
Emmanuel Díaz Leal Hernández
Traccar Course
Access the Traccar course here: Traccar Course
You can filter data if you want to save disk space before it arrives at the Traccar Filter Configuration Guide.
Deleting Old Data in Traccar
If you have space issues or just want to delete old data, you can see how to purge old data in Traccar.
When you start having many devices or they have been active for a long time, Traccar can use a lot of disk space. Therefore, purging this data periodically is very important.
1. Manual Methods to Delete History in Traccar
Manually deleting the tracking history in Traccar is simple and helps maintain system efficiency. Here is how to do it:
Access the Database: Use a database client like phpMyAdmin or the command line:
mysql -u root -pIdentify the Database: In our case, it is
traccar.USE traccar;Run the following command: This will delete all records older than 180 days (approx. 6 months):
Usually, you will only need to delete positions.
delete from tc_positions where servertime < DATE(NOW() - INTERVAL 180 day);But you might also be interested in deleting other things like statistics and events:
delete from tc_positions where servertime < DATE(NOW() - INTERVAL 180 day); delete from tc_statistics where capturetime < DATE(NOW() - INTERVAL 180 day); delete from tc_events where servertime < DATE(NOW() - INTERVAL 180 day);Confirm the Deletion: You will see a message with the number of affected rows.
Query OK, 1050 rows affected (1.85 sec)
2. Setting Up a Cron Job for Automatic Deletion
Automating the deletion of old data using cron jobs can save time. Follow these steps:
Create a Deletion Script:
nano /etc/cron.daily/traccar-clear-database#!/bin/bash result="" while [[ "$result" != *" 0 rows affected"* ]]; do result=$(mysql -u root -proot traccar -vve "DELETE FROM tc_positions WHERE fixTime < DATE(DATE_ADD(NOW(), INTERVAL -90 DAY)) AND id NOT IN (SELECT positionId FROM tc_devices WHERE positionid IS NOT NULL) LIMIT 10000") sleep 1 done result="" while [[ "$result" != *" 0 rows affected"* ]]; do result=$(mysql -u root -proot traccar -vve "DELETE FROM tc_events WHERE eventTime < DATE(DATE_ADD(NOW(), INTERVAL -90 DAY)) LIMIT 10000") sleep 1 doneMake it executable:
chmod +x /etc/cron.daily/traccar-clear-databaseMake sure to adjust parameters according to your use case:
The length of the database history is controlled by INTERVAL -90 DAY. In the example, we delete values older than 90 days.
Keep in mind that the limit date is rounded to a date.
The database name (traccar), username (root), and password (root) should also be updated.
It is strongly recommended to create indices on tc_positions.fixTime and tc_events.eventTime as follows:
CREATE INDEX idx_positions_fixtime ON tc_positions (fixtime); CREATE INDEX idx_events_eventtime ON tc_events (eventtime);
3. Clear Logs: The following command will perform a daily deletion of Traccar logs.
cat > /etc/cron.daily/traccar-clear-logs << EOF
#!/bin/sh
find /opt/traccar/logs/ -mtime +3 -type f -delete
EOF
chmod +x /etc/cron.daily/traccar-clear-logs
Quick Tips
- Regular Backup: Perform database backups before deleting data.
- Post-Operation Verification: Verify the correct operation of the system after deletion.
- Documentation: Document the scripts and configurations used.