🧾 Intro (Paragraph Block):
If you’re running GoAccess on your CWP (Control Web Panel) server to generate traffic statistics for multiple users, your disk can quickly fill up with daily, weekly, and monthly logs.
Here’s a POSIX-compliant shell script that automatically cleans old GoAccess logs for all users in /home/, based on a configurable retention policy:
- ✅ Daily logs → Retain for 30 days
- ✅ Weekly logs → Retain for 8 weeks
- ✅ Monthly logs → Retain for 6 months
You can change the retention values as needed via environment variables.
🛠 Step 1: Save the Script (Code Block)
Use a Code Block for this entire script:
#!/bin/sh
# ========================
# POSIX GoAccess Log Cleanup Script
# ========================
# Default retention values (can be overridden by environment or cron)
DAILY_RETENTION_DAYS=${DAILY_RETENTION_DAYS:-60}
WEEKLY_RETENTION_DAYS=${WEEKLY_RETENTION_DAYS:-90}
MONTHLY_RETENTION_DAYS=${MONTHLY_RETENTION_DAYS:-366}
START_TIME=$(date "+%Y-%m-%d %H:%M:%S")
echo "============================================================"
echo "[INFO] GoAccess Log Cleanup Started: $START_TIME"
echo "Retention Policy:"
echo " - Daily : $DAILY_RETENTION_DAYS days"
echo " - Weekly : $WEEKLY_RETENTION_DAYS days"
echo " - Monthly : $MONTHLY_RETENTION_DAYS days"
echo "============================================================"
total_deleted=0
total_scanned=0
# POSIX cleanup function
cleanup_logs() {
path="$1"
days="$2"
label="$3"
deleted_count=0
if [ -d "$path" ]; then
echo " ──> [$label] Scanning $path for files older than $days days..."
find "$path" -type f -mtime +"$days" -print | while IFS= read -r file; do
echo " [DEL] $file"
rm -f "$file"
deleted_count=$((deleted_count + 1))
total_deleted=$((total_deleted + 1))
done
echo " ✔ Deleted $deleted_count old files from $label"
else
echo " Skipping missing directory: $path"
fi
}
# Loop through each user GoAccess directory
for go_dir in /home/*/cwp_stats/goaccess; do
if [ -d "$go_dir" ]; then
echo ""
echo "============================================================"
echo "[USER] Processing: $go_dir"
cleanup_logs "$go_dir/daily" "$DAILY_RETENTION_DAYS" "Daily"
cleanup_logs "$go_dir/weekly" "$WEEKLY_RETENTION_DAYS" "Weekly"
cleanup_logs "$go_dir/monthly" "$MONTHLY_RETENTION_DAYS" "Monthly"
echo "[DONE] Finished processing: $go_dir"
echo "============================================================"
total_scanned=$((total_scanned + 1))
fi
done
END_TIME=$(date "+%Y-%m-%d %H:%M:%S")
echo ""
echo "Cleanup Summary"
echo " - Users scanned : $total_scanned"
echo " - Files deleted : $total_deleted"
echo " - Started at : $START_TIME"
echo " - Finished at : $END_TIME"
echo "============================================================"
🔧 Step 2: Make It Executable (Code Block)
Use a new Code Block:
chmod +x /opt/goaccess/goaccess-cleanup.sh
⏰ Step 3: Add Cron Job (Code Block)
crontab -e
Then add this line:
0 0 * * * /opt/goaccess/goaccess-cleanup.sh >> /var/log/goaccess_cleanup.log 2>&1
Or with custom retention values:
0 0 * * * DAILY_RETENTION_DAYS=60 WEEKLY_RETENTION_DAYS=90 MONTHLY_RETENTION_DAYS=365 /opt/goaccess/goaccess-cleanup.sh >> /var/log/goaccess_cleanup.log 2>&1
🧪 Step 4: Test It Manually (Code Block)
sh /opt/goaccess/goaccess-cleanup.sh
✅ Why Use This?
This keeps your server clean and prevents stale GoAccess stats from bloating disk space, especially in multi-user environments. It’s:
- ✔ POSIX-compliant (runs with
/bin/sh) - ✔ Fully automatic
- ✔ Logs every action for transparency
- ✔ Works with all CWP users’ GoAccess directories under
/home/*