Skip to main content

How to delete log files automatically to clear up disc space

By default, on new instances, we auto-configured to delete the log files automatically daily, but if your VM doesn't have these file or you deleted it then you can easily configure it by following the below steps. 

To create a script to delete all logs on your VM first open the terminal from the dashboard you can find it inside the service details top right side.

After opening a terminal paste the below code to create the script for deleting log files.

#Create maintenance daily log removing script
echo 'echo "$(tail -c 30m /tmp/appStack.log)" > /tmp/appStack.log' >> /opt/maintenance-daily.sh
echo 'echo "$(tail -c 30m /var/log/syslog)" > /var/log/syslog' >> /opt/maintenance-daily.sh
echo 'journalctl --vacuum-time=10d --vacuum-size=500M;' >> /opt/maintenance-daily.sh

#Provide executable permissions to script
chmod +x /opt/maintenance-daily.sh

#Create a cron job on VM to run these scripts on daily basis.
crontab -l | { cat; echo "0 1 * * * /opt/maintenance-daily.sh"; } | crontab -

You can also change these cron execution times by replacing   0 1 * * *   them with your custom cron time values.