Automated Cleanup of Rotated Apache Error Logs: Safe Deletion Methods and Cron Solutions


2 views

Apache typically uses logrotate to manage log files, creating compressed archives like error.log.2.gz when rotating. These files accumulate over time and can consume significant disk space.

Yes, it's completely safe to delete rotated log files (error.log.1 through error.log.11.gz) as long as:

  • You're not currently troubleshooting issues that might require historical logs
  • Apache isn't writing to these files (they're rotated/compressed copies)

To quickly remove old logs while keeping recent ones:

sudo find /var/log/apache2/ -name "error.log.*.gz" -mtime +30 -delete

This deletes gzipped error logs older than 30 days.

Using logrotate Configuration

Edit /etc/logrotate.d/apache2:

/var/log/apache2/*.log {
    daily
    missingok
    rotate 7
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
        /etc/init.d/apache2 reload > /dev/null
    endscript
}

Key parameters:

  • rotate 7 - keeps only 7 rotated logs
  • compress - enables gzip compression

Cron-based Cleanup

Add this to your crontab (crontab -e):

0 3 * * * find /var/log/apache2/ -name "error.log.*" -mtime +14 -exec rm {} \;

Runs daily at 3 AM, removing error logs older than 14 days.

This script maintains 10GB of logs maximum:

#!/bin/bash
LOG_DIR="/var/log/apache2"
MAX_SIZE=10240 # 10GB in MB

current_size=$(du -sm "$LOG_DIR" | cut -f1)
while [ "$current_size" -gt "$MAX_SIZE" ]; do
    oldest_file=$(find "$LOG_DIR" -name "error.log.*" -printf '%T+ %p\n' | sort | head -n 1 | awk '{print $2}')
    [ -z "$oldest_file" ] && break
    rm -f "$oldest_file"
    current_size=$(du -sm "$LOG_DIR" | cut -f1)
done
  • Always test deletion commands with -ls before -delete
  • Consider archiving important logs before deletion
  • Monitor disk space after implementing changes

Yes, you can safely delete rotated Apache error logs (error.log.1 through error.log.11.gz). These are historical logs that have been rotated by logrotate or Apache's built-in rotation mechanism. The current active log file (error.log) should never be deleted while Apache is running.

Apache typically uses one of these log rotation methods:

  1. Built-in rotation (when using CustomLog/ErrorLog directives with rotation parameters)
  2. System's logrotate utility (common in Debian/Ubuntu systems)

For immediate space recovery, run:

# Delete all rotated error logs older than current
sudo find /var/log/apache2 -name "error.log.*" -delete

# Alternative with compression check
sudo find /var/log/apache2 -name "error.log.[0-9]*" -o -name "error.log.[0-9]*.gz" -delete

Edit your Apache logrotate configuration (typically at /etc/logrotate.d/apache2):

/var/log/apache2/*.log {
    daily
    missingok
    rotate 7
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
        /etc/init.d/apache2 reload > /dev/null
    endscript
}

Key parameters to modify:

  • rotate 7 - keeps 7 rotated logs (reduce this number)
  • compress - enables gzip compression
  • daily - rotation frequency

For systems without logrotate:

# Add to crontab (crontab -e)
0 2 * * * find /var/log/apache2 -name "error.log.*" -mtime +30 -delete

After configuration:

# Test logrotate configuration
sudo logrotate -d /etc/logrotate.d/apache2

# Force immediate rotation
sudo logrotate -vf /etc/logrotate.d/apache2

Set up monitoring to prevent future issues:

# Simple disk space check script
#!/bin/bash
THRESHOLD=90
CURRENT=$(df /var/log | awk '{print $5}' | tail -1 | sed 's/%//')

if [ "$CURRENT" -gt "$THRESHOLD" ]; then
    echo "Cleaning Apache logs..."
    find /var/log/apache2 -name "*.gz" -mtime +7 -delete
fi