How to clean up Ubuntu server disk space

0 Shares
0
0
0
0

Introduction

Is your Ubuntu server running out of disk space?

Would you like to clean it up and free up some space? If so, you've come to the right place.

Many developers face the same problem like you, they deployed one or two small applications on their Ubuntu server, but over time the disk space gets filled up and they are confused about what to do and how to free up their disk space.

I was in a similar situation recently, I had an Ubuntu server that was at 93% of its disk capacity and the disk usage for a small Django application running on the server was around 39GB. I knew getting additional disk space wasn't an option because the application files themselves were less than 1GB.

Ultimately, I had to spend a day going through the entire server and determining which directories and programs were taking up my disk space.

In this article, I will share my proven steps to find unnecessary files and clean up 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 terms
  • Username: holu (your server username)
  • Host name:

How to clean up Ubuntu server disk

When it comes to disk space usage, the biggest culprit is usually logs. The Ubuntu operating system keeps a log of almost everything, and many developer and server tools and frameworks, like Celery and Nginx, also create and store logs on your server.

These reports, generated by various tools and packages, are usually many gigabytes in size, eating up your disk space and slowing down your server.

Before we get into the practical steps, I want to point out that you should not delete the root system log folder. Never be tempted to just delete the root log folder (/var/log/). If you do, you will break a lot of things that you don't want to break.

Step 1 – Check disk space

The first step is to access the Ubuntu server via SSH. You can log in using the following command:

Holu with your username and Replace with your server IP.

ssh holu@<your_host>

After logging in, run the following command to view your disk space usage.

sudo du -cha --max-depth=1 / | grep -E "M|G"

Sample 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 high-level details about the size of various folders/directories on the disk.

If you look at the sample output above, the top users of our disk space are /root, /usr, /home, and /var. The var directory alone is 11G, which is a lot of space used by that directory.

Step 2 – Go to the var directory

The next step is to enter the /var folder using the following command.

cd /var

Next, run the following command to see the disk space usage for all the folders inside the /var folder. This will help us figure out which folders to start our cleanup work with.

sudo du -bsh *

Sample output:

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 run that command on my server, I can see that the lib and log directories are using the most space in the /var folder.

With this information, I can focus on both the lib directory and the log directory to free up some space on my hard drive.

Step 3 – Go to each directory to delete it

The next thing is to go into these directories and clean them up. For the purpose of this article, I will only focus on the log directory, but know that you can apply the same steps we apply here to clean up any other directory on your Ubuntu server.

To be able to clear the log directory, we need to delete the old log file and other unnecessary files on the server. To access the log directory, run the following command.

cd log

Then do:

ls

Sample output:

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 

The ls command shows us all the files in this directory and as you can see there are a lot of archived log files in this directory.

Then check the size of all files and folders in this directory using the following command.

sudo du -bsh *

This is the same command we used above, so you should see a list of all the files and their sizes. Make a note of the journal folder.

Step 4 – Delete old log files

The next thing we do is delete all the old log files. Old log files are easy to identify, they usually end with the .gz file name extension.

We will use a command to find all .gz files and delete them from our server.

Run the following command 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

Do it now:

ls

You should see a deleted log directory. You can also modify this command to delete different file types by changing the value of the -name attribute.

We're not done yet because the journal directory is using up a lot of disk space. So, we need to check this directory and clean up some files.

cd journal

Inside the journal directory, you will see a folder that is usually named with a series of numbers. To delete it, we can use the following command.

This command will reduce the size of the journal directory to 100 MB, which is a reasonable size.

sudo journalctl --vacuum-size=100M

If you want a permanent fix to using a journal disk, follow the process below.

sudo nano /etc/systemd/journald.conf

In the configuration file, remove the SystemMaxFileSize and SystemMaxFiles settings (remove #) 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 the extra logs.

Step 5 – Check the /var/lib folder

In my experience, another big space user is the /var/lib directory. Depending on the number of libraries or tools installed on your Ubuntu server, this directory can take up a lot of space.

A quick way to find out which libraries are taking up a lot of space is to run the following command.

sudo du -hs /var/lib/* | sort -hr | head -10

This command will show you the 10 largest library folders in this directory, you can then use this information to continue your cleanup process.

You should completely remove libraries or tools that you are not currently using.

Conclusion

As you can see, cleaning up your Ubuntu server is not that difficult. By following the steps above, we have successfully cleaned up our /var/log/ directory. Follow the same steps to clean up other directories in the /var folder and you can free up a lot of space on your Ubuntu server.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like