5 Most Effective Ways to Free Disk Space on Ubuntu Server


With a desktop environment, you can generally get some great tools to help you analyze your disk usage and quickly find ways to free some disk space on your system. On a server, it’s not as straightforward, but there are a few useful commands to know that can be even more powerful for this. Let’s take a look at them.

On Linux servers, the commands “du” and “find” can help to find the biggest files on the system in order to quickly free disk space by removing them.

But they don’t always work (especially if your disk is already 100% full), so let’s examine the 5 methods I tested for you in this article.

Boost your Raspberry Pi performance...
Boost your Raspberry Pi performances with these 6 simple tips - Overclocking, SSD & more

1 – Remove the biggest files on your system

Get My Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

The fastest way to free up some space on Ubuntu server is to remove the largest files on the system. Videos, installation packages and ISO files can be over 1 GB each, taking space quickly on your server.

When I was a system administrator in a company, I used this command almost every day:
sudo du -ak | sort -nr | head -50
Let’s break it down:

  • du -ak: list all files in the current folder, with the corresponding size in KB.
  • sort -nr: sort the list in the reverse order (from the biggest file to the smallest one).
  • head -50: only keep the 50 biggest files. Don’t hesitate to change this number if needed.

So, when you want to find the biggest file on your system, move to the root of your hard drive, or in a specific folder, and run the previous command. It will give you something like:

By using this command, I quickly find an ISO file in my home directory, using more than 3 GB.
If I don’t need it anymore, I can easily remove it to free some disk space, by using the “rm” command:
rm /home/pat/ubuntu-22.04.1-desktop-amd64.iso

There is one issue with this command, though. It creates a temporary file to sort the list by size, so it won’t work if your disk is already full. You’ll get that kind of error: “write failed: No space left on device”.

In this case, there is another command you can try to find the biggest file on your system: “find”.
Yes, the command name is easy to remember. But here is a good option you can add to find files bigger than a specific size:
sudo find / -size +1G

It will list all the files on the whole drive, taking over 1 GB of disk space.
By doing this, I’ll also find my ISO file:

You can adjust the path and the minimum size in the command line, depending on your needs.
For example:
find /home -size +100M
sudo find /media/share -size +10G

You can even use this command to find files from a certain type (with their extension for example):
sudo find / -iname *.mkv
find /home/pat -iname *.iso

This solution should already help a lot, but you’ll only find the biggest files. It doesn’t work for folders and applications. That’s why you may still need to check the other solutions listed below.

2 – Uninstall unused applications

Raspad 3
Learn & program in Mins
A tablet that gives you access to all the Raspberry Pi ports you need, with a simple interface to start programming easily.

Buy now Check my review

In most cases, the disk space is mainly taken by large applications installed on the server. The dpkg-query command can help us to list the applications that are taking up the most space.

The full command looks like this:
dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -nr | head -20

It’s similar to the previous one, but instead of listing all the files, we list all the installed packages and sort the list by size. The output on Ubuntu Server looks like that:

This way, I can quickly identify that aside from the Linux kernels, I have a few applications that are taking up some space on my drive, like Java, MySQL, and PHPMyAdmin. I don’t have large applications installed on this example, but uninstalling one of them can help me quickly go under 100% disk usage.

I can use apt to remove one of the applications, following the syntax:
sudo apt remove <package>
So, for example:
sudo apt remove openjdk-18-jdk-headless

This will free up 234 MB of disk space, and maybe allow me enough time to restart my services and investigate a bit more to free more space later. For example, if my disk was 100% full, I can now use the first method given in this article to find the biggest file, and I won’t get the “disk full” error anymore.

3 – Remove unused packages

On Debian-based distributions like Ubuntu, APT is the package manager. It’s great because it will install automatically the required dependencies. But it won’t necessarily remove them when you don’t need them anymore, which can quickly take some unnecessary space on your hard drive.

Get My Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

For example, in the previous solution, I uninstalled Java. When I installed it, it came with many additional dependencies that were installed at the same time. I don’t need them anymore, but apt didn’t remove them when I uninstalled Java.

To clean all these packages that are no longer needed on your system, you can use this command:
sudo apt autoremove --purge

As you can see on the screenshot, it will uninstall a bunch of packages that are no longer required.
I saved only 234 MB by removing Java, but I will save 444 MB more of disk space by cleaning all the dependencies.

Don’t forget to do this from time to time, it’s an easy win when you need to quickly get more free space.

The last two solutions I have for you won’t necessarily free up so much space, but they are also quick ways to unstuck you from a “no space left on disk” issue.

4 – Delete log files

By default, Ubuntu Server keeps a history of all log files. Each log file rotates following this pattern:

  • The current log file, for example, “dmesg”, with the latest entries.
  • The previous file, for example, “dmesg.0”, with the entries from the previous main file.
    Depending on the system configuration, it may rotate every day, or when the maximum file size is reached.
  • Archives files, for example, “dmesg.1.gz”, zipped with the content of the previous files.
    Your system will generally keep up to 5 archive files.

Log files are stored under /var/log, it look like this:

On a clean system like on my screenshot, this shouldn’t take much space. But if you have a server with high traffic that generates a log entry for each visitor, page, or hit, it can be way more than that.

But anyway, removing the archives files, for example, can give you enough space to use the first command in this list, especially when the disk is currently full.
You can quickly remove these log files with:
sudo rm /var/log/*.gz
Or:
sudo rm /var/log/*.1

Don’t forget to check the subfolders too. For example, the “apache2” subfolder can take a lot of space if you are hosting a big website like RaspberryTips :-).

You can also move these files to an external drive or share them if you are unsure if you’ll need them later or not.

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

5 – Clear the APT cache

Get My Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

The last thing you can try to free a few megabytes quickly on your hard drive is to clean the APT cache. Each time you use APT, Ubuntu will store the repository content on your disk, taking some space that is not essential.

APT stores this cache under /var/cache/apt, and you can clear it safely with this command:
sudo apt clean

During my tests, this command helped me free up about 80 MB of disk space. That’s not much, but it can help to restart the essential services and take more time to find the real issue with the disk space currently used.

Related questions

How to check the current disk space usage on Ubuntu Server?

On Ubuntu Server, the command “df” list all the partitions with their size and how much is currently used and available. The “-h” option makes it readable, by showing MB or GB instead of 1K-blocks.

Keep in mind that this percentage of use won’t necessarily be refreshed in real time, especially when the disk is full. But it can give you an idea and clearly help to find if there is a “storage full” issue or if it’s something else.

How much space does Ubuntu Server need?

According to the official website, Ubuntu’s minimum requirement is 8 GB for the main partition plus the size of your RAM and 250 MB to 1 GB for the boot partition.

I already wrote an article on this topic, so if you want more details, I encourage you to read it, you can find it here: How Much Space Does Ubuntu Need? (All versions)

And I also have a lot of interesting articles about Ubuntu in general that you might want to read too:

Patrick Fromaget

I'm the lead author and owner of RaspberryTips.com. My goal is to help you with your Raspberry Pi problems using detailed guides and tutorials. In real life, I'm a Linux system administrator with a web developer experience.

Recent Posts