Log Cleanup
Automated daily cleanup of log and history tables based on the global log retention setting.
Overview
The log cleanup system removes old records from log and history tables to prevent unbounded database growth. It runs as a daily external cron job at 3 AM.
Tables Cleaned
| Table | Retention | Description |
|---|---|---|
rm_webhook_log | log_retention_days (default 30) | Product/inventory webhook request logs |
rm_item_history | log_retention_days (default 30) | Item sync change history |
rm_navision_request_history | log_retention_days (default 30) | Business Central API request logs |
rm_error_orders | 2× log_retention_days (default 60) | Failed order records (kept longer for troubleshooting) |
Configuration
The retention period is controlled by the Log Retention Days setting in GSM Middleware → Settings.
- Default: 30 days
- Range: 1–365 days
- Error orders are automatically kept for double the retention period
How It Works
- External cron triggers
cron/cleanup-logs.phpat 3 AM daily - The script bootstraps WordPress and loads plugin settings
- For each table, records older than the retention threshold are deleted
- Per-table success/error handling prevents one table failure from blocking others
- Completion summary is written to stdout with total records deleted
Cron Setup
The cleanup cron entry is automatically included when you generate the crontab via the Crontab Generator:
# --- Log Cleanup (daily at 03:00) ---
0 3 * * * /usr/bin/php8.2 /path/to/wp-content/plugins/gsm-middleware/cron/cleanup-logs.php
Manual Execution
Run the cleanup script manually from the command line:
php /path/to/wp-content/plugins/gsm-middleware/cron/cleanup-logs.php
Output example:
GSM Middleware Log Cleanup
==========================
Retention: 30 days
Deleted 142 rows from rm_webhook_log
Deleted 89 rows from rm_item_history
Deleted 56 rows from rm_navision_request_history
Deleted 12 rows from rm_error_orders (60-day retention)
==========================
Total deleted: 299 rows
Completed in 0.34 seconds
Troubleshooting
Script Fails to Run
- Verify PHP path is correct in crontab
- Ensure WordPress loads successfully:
php /path/to/wp-load.php - Check that the GSM Middleware plugin is active
Tables Not Getting Cleaned
- Verify the cron entry exists:
crontab -l | grep cleanup - Check the
log_retention_dayssetting value in Settings - Run manually and check output for errors
Related Documentation
- Crontab Generator — Generate and manage cron entries
- Settings — Configure log retention period
- Performance Optimization — Database performance tuning