Prerequisites
Before deleting old logs, make sure you have:
- SSH access to your VPS
- Root or sudo privileges
Method 1: Using logrotate rotate count
Connect to your VPS:
ssh hxroot@YOUR_SERVER_IP -p 22
Edit /etc/logrotate.conf:
rotate 30 # Keep 30 rotations (daily = 30 days)
Or for weekly rotation:
rotate 4 # Keep 4 weeks
Method 2: Cron Job with find
Create cleanup script:
sudo nano /usr/local/bin/cleanup-logs.sh
#!/bin/bash
find /var/log -type f -name "*.log" -mtime +30 -delete
find /var/log -type f -name "*.gz" -mtime +90 -delete
find /var/log -type f -name "*.1" -mtime +30 -delete
sudo chmod +x /usr/local/bin/cleanup-logs.sh
Add to cron:
sudo crontab -e
0 3 * * * /usr/local/bin/cleanup-logs.sh
Method 3: Delete Specific Old Journal Logs
sudo journalctl --vacuum-time=30d
Method 4: Dry Run Before Deletion
find /var/log -type f -name "*.log" -mtime +30 -ls
Review then remove the -ls and add -delete.
✅ Old logs will be automatically deleted after 30 days.