Introduction
Is your Ubuntu server disk space getting filled up?
Would you love to clean it up and free up some space? If yes, you are at the right place.
Many developers like you are experiencing the same problem, they deployed one or two small apps on their Ubuntu server but over time, the disk space is getting filled up and they are confused about what to do and how to free up their disk space.
Recently, I was in a similar situation, i had an Ubuntu server that was at 93% of its disk capacity, and the disk usage was about 39GB for one small Django app that was running on the server. I knew getting additional disk space was not an option since the application files itself was less than 1GB.
Eventually, I had to dedicate a day to checking the whole server and determining which directories and applications were taking up my disk space.
In this article, I will share my proven steps for finding unneeded files and cleaning up your Ubuntu server disk space.
Prerequisites
- Server with Ubuntu (e.g. with Hetzner Cloud)
- SSH access to the server
- Access to the root user or a user with sudo permissions
Example terminology
- Username:
holu
(your server username) - Hostname:
<your_host>
How to Cleanup Your Ubuntu Server Disk
When it comes to disk space consumption, the biggest culprits are usually logs. The Ubuntu OS keeps a log of almost everything and many dev and server tools and frameworks such as Celery and Nginx also create and store logs on your server.
These logs created by various tools and packages usually run into huge amounts of Gigabytes, eating up your disk space and making your server slower.
Before we proceed into the practical steps, I would like to mention that you should not delete the root system log folder. Never be tempted to just delete the root log (/var/log/
) folder. If you do this, you will break a lot of things that you don’t want to break.
That said, let’s get started.
Step 1 - Check disk space
The first step is to access your Ubuntu server via SSH. You can log in by using the command below:
Replace holu with your own username and
<your_host>
with the IP of your server.
ssh holu@<your_host>
Once you are logged in, execute the command below to view your disk space usage.
sudo du -cha --max-depth=1 / | grep -E "M|G"
Example output:
holu@<your_host>:~$ sudo du -cha --max-depth=1 / | grep -E "M|G"
2.8G /root
5.4G /usr
53M /tmp
9.8M /etc
203M /boot
du: cannot access '/proc/100209/task/100209/fd/4': No such file or directory
du: cannot access '/proc/100209/task/100209/fdinfo/4': No such file or directory
du: cannot access '/proc/100209/fd/3': No such file or directory
du: cannot access '/proc/100209/fdinfo/3': No such file or directory
2.5G /home
1.1M /run
307M /opt
522M /logs
11G /var
1.3G /snap
24G /
24G total
As you can see, this command shows us a top-level detail about the size of various folders/directories on the disk.
If you look closely at the example output above, our top disk space users are /root
, /usr
, /home
, and /var
.
The var directory alone is 11G, that is a lot of space being used by that directory.
Step 2 - Move into the var directory
Next step is to move into the /var
directory using the command below.
cd /var
Next, execute the command below to see the disk space usage for all folders within the /var
directory. This will help us to know which folders to start our cleanup task from.
sudo du -bsh *
Example outout:
holu@<your_host>:/var$ sudo du -bsh *
2.7M backups
150M cache
4.0K crash
3.6G lib
4.0K local
9 lock
2.5G log
3.0G mail
4.0K opt
4 run
53K snap
52K spool
44K tmp
460M www
When I execute that command on my server, I can see the lib and log directories are using the most space in the /var
directory.
With this information, I can focus on both the lib
directory and log
directory to free up some space on my hard disk.
Step 3 - Move into each directory to clean it up
The next thing is to get into these directories and clean them up. For the purpose of this article, I will be focusing only on the log
directory, but know that you can apply the same steps that we apply here to clean up any other directory on your Ubuntu server.
For us to clean up the log directory, we need to remove the old log field and other unnecessary files on the server. Execute the command below to access the log directory.
cd log
Then do:
ls
Example outout:
holu@<your_host>:/var$ cd log
holu@<your_host>:/var/log$ ls
alternatives.log fail2ban.log php7.4-fpm.log.5.gz
alternatives.log.1 fail2ban.log.1 php7.4-fpm.log.6.gz
alternatives.log.2.gz fail2ban.log.2.gz php7.4-fpm.log.7.gz
alternatives.log.3.gz fail2ban.log.3.gz php7.4-fpm.log.8.gz
alternatives.log.4.gz fail2ban.log.4.gz php7.4-fpm.log.9.gz
alternatives.log.5.gz faillog postgresql
alternatives.log.6.gz fontconfig.log private
apport.log installer supervisor
apport.log.1 journal syslog
apport.log.2.gz kern.log syslog.1
apport.log.3.gz kern.log.1 syslog.2.gz
apport.log.4.gz kern.log.2.gz syslog.3.gz
apport.log.5.gz kern.log.3.gz syslog.4.gz
apport.log.6.gz kern.log.4.gz syslog.5.gz
apport.log.7.gz landscape syslog.6.gz
apt lastlog syslog.7.gz
The ls
command will show us all files in this directory and as you can see there are a lot of archived log files in this directory.
Next, check the size of all files and folders in this directory using the command below.
sudo du -bsh *
This command is the same one we used above, so you should see a list of all files and their sizes. Take note of the journal
folder.
Step 4 - Remove old log files
The next thing we will do is delete all old log files. Old log files are easy to detect, they usually end with a .gz
file name extension.
We will be using a command to find all .gz
files and delete them from our server.
Execute the command below to find and delete all .gz
and .log.1
files in the log directory.
sudo find -type f \( -name "*.log.1" -o -name '*.gz' \) -delete
Now, do:
ls
You should see a cleaned-up log directory. You can also modify this command to delete various file types by changing the -name
attribute value.
We are not done yet because the journal directory is using a lot of disk space. So, we need to check out this directory and clean up some files.
cd journal
Inside the journal directory, you will find a folder usually named with a series of numbers. To clean this up, we can use the command below.
This command will reduce the size of the journal directory to 100MB, which is a fair size.
sudo journalctl --vacuum-size=100M
If you want a permanent fix for the journal disk usage, follow the process below.
sudo nano /etc/systemd/journald.conf
In the configuration file, go down to uncomment (remove #
) the SystemMaxFileSize
and SystemMaxFiles
settings and set them to 100. So you should have:
SystemMaxFileSize=100
SystemMaxFiles=100
Next, do:
sudo service systemd-journald restart
This will apply the changes and remove excess logs.
Step 5 - Inspect the /var/lib directory
Based on my experience, another big user of space is the /var/lib
directory. This directory can take up so much space depending on the amount of libraries or tools installed on your Ubuntu server.
A quick way to figure out which libraries are using up a lot of space is to run the command below.
sudo du -hs /var/lib/* | sort -hr | head -10
This command will show you the top 10 biggest library folders in this directory, you can then use this information to proceed with your cleanup process.
You should completely remove any library or tools that you are not using at the moment.
Conclusion
As you can see, cleaning up an Ubuntu server is not that difficult. By following the steps above, we have successfully cleaned up our /var/log/
directory. To also clean up other directories in the /var
directory, follow the same steps and you will be able to free up a lot of space on your Ubuntu server.