Clean up Ubuntu server
Recently I need to downsize a DigitalOcean droplet to save cost. However the server used disk is much higher than I thought and I need to clean it up before snapshotting it.
Turn out there are more to consider that just few basic rm
commands to identify and remove unused files/directories. This articles briefly summarizes several ways to clean up an Ubuntu server.
Remove unused dependencies / softwares / software versions
Remove unused system dependencies
apt-get autoremove
Remove unused softwares / software versions
When we use program version managers, e.g. rvm
to manage ruby versions, nvm
to manage node versions, we may have multiple software versions installed along with their libraries. This often happens when we upgrade application, e.g. upgrading Rails app to a newer Ruby version. Ensure that we remove the unused versions to save space. In some cases their libraries are cached by other programs (e.g. Capistrano caches Ruby gems) and we need to ensure those are removed as well.
Remove cached files
Lots of programs use cache to improve their performance and those caches may not be removed automatically. Ensure to check cache directories (they're often in ~/.cache
) and remove unnecessary ones.
Find largest directories / files
Use du
command
sudo du / -h --max-depth=1
Use ncdu program
ncdu
is a command line TUI that allows us to analyze diskspace used by the server with a friendly UI to browse files and directories.
To install ncdu
sudo apt-get install ncdu
To analyze a directory
ncdu /path/to/directory
Log files
Remove/Empty log files
Using the tool above we could find big log files and remove them. However in some cases it is prefered to keep the log file in place and just empty it.
A log file could be emptied by using echo
command
echo '' > /path/to/log
Some log files can only be deleted or overwritten by root
user or sudo
command. For example nginx access log file:
sudo rm /var/log/nginx/access.log
However the file will not be recreated until nginx is reloaded or restarted:
sudo service nginx reload
So the better solution is to resize the file to 0 bite
sudo truncate --size 0 /var/log/nginx/access.log
Use logrotate to manage log files
Remove or empty the log files is just a temporary action to free up disk space. To avoid the continuous growing log files we should use a tool to manage these logs. logrotate is a Linux utility that will do that
logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.
We should use logrorate
for applications that generate lots of logs like web servers (nginx, Apache), application servers (Rails, Django, etc.).