1. Delete Journal records:
journalctl --disk-usage sudo journalctl --rotate sudo journalctl --vacuum-time=2days sudo journalctl --vacuum-size=100M sudo journalctl --vacuum-files=5 sudo -H gedit /etc/systemd/journald.conf systemctl daemon-reload
2. Check for heavy files and then clean:
sudo du -a / 2>/dev/null | sort -n -r | head -n 20 sudo apt-get autoremove sudo du -sh /var/cache/apt sudo apt-get autoclean sudo find / -xdev -type f -size +100M //Command to check files greater than 100MB. SET GLOBAL binlog_expire_logs_seconds = 3; //Command in Mysql expire-logs-days=3 PURGE BINARY LOGS TO 'binlog.000113'; //Command in Mysql
3. Create an automated Script in /var/www folder and name it as cleanup.sh:
#!/bin/bash # Log rotation and cleaning journal logs echo "Rotating journal logs..." sudo journalctl --rotate echo "Vacuuming journal logs older than 2 days..." sudo journalctl --vacuum-time=2days echo "Vacuuming journal logs to limit size to 100MB..." sudo journalctl --vacuum-size=50M # Remove unnecessary packages echo "Removing unnecessary packages..." sudo apt-get autoremove -y # Clean package cache echo "Cleaning up package cache..." sudo apt-get autoclean # Check disk usage of cached apt packages echo "Checking apt cache size..." sudo du -sh /var/cache/apt # Finding and deleting large .bin files in MySQL directory echo "Finding and deleting .bin files larger than 50MB in MySQL folder..." sudo find /var/lib/mysql -xdev -type f -name "*.bin" -size +50M -delete # Clean Docker (if Docker is installed) if [ -x "$(command -v docker)" ]; then echo "Cleaning up Docker system..." sudo docker system prune --volumes -f else echo "Docker is not installed, skipping Docker cleanup." fi echo "Cleanup complete!"
After this, run following commands:
chmod +x /var/www/cleanup.sh export EDITOR=nano crontab -e 0 0 * * * /var/www/cleanup.sh >> /var/log/cleanup.log 2>&1 crontab -l
TO manually run this,
1. cd /var/www/
2. sudo ./cleanup.sh
In the crontab editor, add the following line to schedule the job to run at 10:30 AM every day:
30 10 * * * /var/www/cleanup.sh >> /var/log/cleanup.log 2>&1
Now, the script will run automatically every day at 10:30 AM, and the output will be logged in
/var/log/cleanup.log