Cleaning up an Ubuntu server

Recently I have run into an HDD space problem with one of my Ubuntu server systems. It’s been up for a few years, without special maintenance from this point of view.

So, here are the steps I have taken:

Cleanup the system:

  • sudo apt-get clean
  • sudo apt-get autoclean
  • sudo apt-get autoremove

Source:

Cleanup the logfiles:

  • shopt -s globstar # if needed
  • truncate -s 0 /var/log/*.log # first-level logs
  • truncate -s 0 /var/log/**/*.log # nested folders, like /var/log/nginx/access.log
  • find /var/log -type f -name '*.[0-99].gz' -exec rm {} +
  • Extra:
    find /var/log -type f -name '*.log.[0-99].gz' -exec rm {} +

Source:
https://askubuntu.com/questions/266738/how-to-truncate-all-logfiles

Clean the btmp file:

  • cat /dev/null > /var/log/btmp

Source:

You can manually delete files that you do not need or which have not been cleaned by the commands above. Anyway, please be careful.

And it is recommended not to delete folders inside /var/logs.