Skip to content

How to Free Up Disk Space on an Ubuntu Server

8 min read

Applies to: Ubuntu 26.04 LTS (apt 3.2.0, systemd 259) and Ubuntu 24.04 LTS (apt 2.8.3), tested; snapd 2.76.3 and BleachBit 5.0.2

An Ubuntu server does not need a cleaner app. First find what is using the space, then clear it with the built-in tools: sudo apt clean empties the package cache, sudo apt autoremove --purge removes unused packages and old kernels, sudo journalctl --vacuum-size=200M shrinks the system log, and old snap revisions and Docker leftovers can be removed with their own commands. Each step below shows what it frees and what to check before you delete anything.

Step 1: Find what is using the space

  1. See which file system is full: df -h. Look at the Use% column for / and any data disks.
  2. List the biggest folders. -x stays on one file system, so other disks and /proc are skipped:
    sudo du -xh --max-depth=1 / 2>/dev/null | sort -h | tail -6
    Repeat with the biggest folder (for example /var) until you find the cause.
  3. List single files over 100 MB:
    sudo find / -xdev -type f -size +100M -exec ls -lh {} + 2>/dev/null

Lab output (Ubuntu 26.04 container with three test files we created):

$ sudo du -xh --max-depth=1 / 2>/dev/null | sort -h | tail -5
122M    /usr
151M    /root
301M    /srv
1.5G    /var
2.0G    /
$ sudo find / -xdev -type f -size +100M -exec ls -lh {} + 2>/dev/null
-rw-r--r-- 1 root root 150M Oct 11 14:38 /root/db-dump.sql
-rw-r--r-- 1 root root 300M Oct 11 14:38 /srv/uploads/video.mp4
-rw-r--r-- 1 root root 1.2G Oct 11 14:38 /var/backups/old/site-backup-2025.tar.gz

Old backups, database dumps and uploads are the usual finds. Move them to another machine or delete them once you are sure you do not need them. For an interactive view, install ncdu (version 1.22 on Ubuntu 26.04) and run sudo ncdu -x /. More options are in how to find the top directories and files.

Step 2: Clear the apt package cache

apt keeps every downloaded .deb file in /var/cache/apt/archives. They are not needed after installation.

sudo apt clean

In our Ubuntu 26.04 lab, after installing 49 packages, /var/cache/apt went from 118 MB to 20 KB. Part of that was the two package index caches (pkgcache.bin and srcpkgcache.bin, 53 MB each), which apt rebuilt the next time it ran. The space you keep is the downloaded packages: 13 MB in that test, often much more on a server that has been updated for months.

sudo apt autoclean is the milder version: it only deletes cached packages that can no longer be downloaded (old versions).

Step 3: Remove unused packages and old kernels

sudo apt autoremove --purge

This removes packages that were installed only as dependencies and are no longer needed, and --purge also deletes their configuration files. Read the list before you answer yes. Lab output after removing jq on Ubuntu 26.04:

REMOVING:
  libjq1*  libonig5*

Summary:
  Upgrading: 0, Installing: 0, Removing: 2, Not Upgrading: 2
  Freed space: 1054 kB

Ubuntu 24.04 (apt 2.8) prints the older style: The following packages will be REMOVED: and After this operation, 1012 kB disk space will be freed.

Old kernels are worth checking, because each one installs its own modules folder under /usr/lib/modules. apt keeps two kernels: the one you are running and the newest other one (the setting is APT::NeverAutoRemove::KernelCount, default 2, as the apt source code shows). We checked the rule with four fake kernel packages on both releases: apt kept the two newest and offered to remove the two oldest. With -o APT::NeverAutoRemove::KernelCount=3 it kept three. Kernels you installed by hand (marked "manual") are never removed automatically. So after a kernel update, reboot first, then run autoremove. To see which kernels are installed and which one is running, see how to check the kernel version.

Leftover configuration from packages removed without purge shows rc in dpkg -l. apt can find and purge them all with the ~c search pattern (tested on Ubuntu 26.04):

apt list '~c'
sudo apt purge '~c'

Step 4: Shrink the systemd journal

The journal in /var/log/journal can grow to 10% of the file system, capped at 4 GB, according to the systemd journald.conf manual. Check it and shrink it:

sudo journalctl --disk-usage
sudo journalctl --vacuum-size=200M     # keep at most 200 MB
sudo journalctl --vacuum-time=2weeks   # or: delete what is older than two weeks

Lab output (Ubuntu 26.04, systemd 259; machine ID shortened):

$ sudo journalctl --disk-usage
Archived and active journals take up 94M in the file system.
$ sudo journalctl --vacuum-size=40M
Deleted archived journal /var/log/journal/MACHINE-ID/system@...journal (24.8M).
(two more lines like this)
Vacuuming done, freed 74.4M of archived journals from /var/log/journal/MACHINE-ID.
$ sudo journalctl --disk-usage
Archived and active journals take up 19.5M in the file system.

Vacuuming only deletes archived journal files, never the active one, and it deletes whole files. If most of the log is still in the active file, rotate first: sudo journalctl --rotate --vacuum-size=200M did both in one command in our test.

To keep the journal small for good, create /etc/systemd/journald.conf.d/size.conf:

[Journal]
SystemMaxUse=200M

Then run sudo systemctl restart systemd-journald. systemd-analyze cat-config systemd/journald.conf shows the setting is picked up. Classic log files in /var/log are handled by logrotate: see log rotation with logrotate.

Step 5: Remove old snap revisions

snapd keeps older revisions of each snap after it updates. List them with snap list --all: old ones show disabled in the Notes column. Remove one revision at a time:

snap list --all
sudo snap remove NAME --revision=REVISION

According to Canonical's snap documentation, refresh.retain sets how many revisions are kept: a number from 2 to 20, default 2 on classic Ubuntu and 3 on Ubuntu Core. sudo snap set system refresh.retain=2 sets the minimum. When you remove a snap, snapd saves an automatic snapshot of its data for 31 days; snap saved lists them and sudo snap forget SET-ID deletes one. snap help remove (snap 2.76.3) confirms that --revision removes only that revision and --purge removes a snap without saving a snapshot. snapd cannot run in our test containers, so these commands come from Canonical's documentation and the snap client's own help.

Step 6: Docker images and build cache (only if you use Docker)

Check first with docker system df, which shows how much is reclaimable. docker system prune removes stopped containers, unused networks, dangling images and build cache, and -a also removes every image not used by a container. Warning: add --volumes only if you are sure, because volumes hold databases and uploads. Our guide to removing Docker containers, images and volumes covers each command with tested output.

Space not freed after deleting a file

If df still shows the disk as full after you deleted a big log, a running program probably still has the file open. The space is only released when the program closes it. Find such files (lab output):

$ sudo lsof +L1
COMMAND PID USER FD   TYPE DEVICE  SIZE/OFF NLINK    NODE NAME
tail    272 root 6r   REG   0,47 419430400     0 1572969 /var/log/app.log (deleted)

Restart the service that holds it. If you cannot restart it now, empty the file through the process: sudo sh -c ': > /proc/272/fd/6' (the PID and FD from the lsof output); in our test the size dropped to 0. Next time, empty a live log with sudo truncate -s 0 /var/log/big.log instead of deleting it.

Other caches people mean by "clear cache"

  • User caches: ~/.cache holds pip, browser and thumbnail caches. Check with du -sh ~/.cache; programs recreate what they need. For npm, see how to clear the npm cache.
  • Memory cache: the "buff/cache" figure in free -h is RAM, not disk space, and Linux frees it by itself. See how to free up Linux memory.

What about cleaner apps such as BleachBit?

BleachBit is a desktop cleaner in the Ubuntu archive (version 5.0.2 on Ubuntu 26.04). On a server it brings a desktop stack with it: our lab install needed 172 packages (144 with --no-install-recommends). Its command line can preview the apt and journal cleaners:

$ bleachbit --preview apt.autoclean apt.autoremove apt.clean journald.clean
journalctl --vacuum-time=1 ?B
apt-get autoclean ?B
apt-get autoremove ?B
apt-get clean ?B

As the preview shows, it runs the same commands as steps 2 to 4, except that its journal cleaner deletes all archived logs (--vacuum-time=1 means older than one second). On a server, running the commands yourself gives you more control.

Do not delete these by hand

  • /var/lib/mysql or /var/lib/postgresql: drop unused databases from inside MySQL, MariaDB or PostgreSQL instead.
  • /var/lib/docker: use the docker commands above.
  • /boot, /usr/lib/modules and /var/lib/dpkg: let apt remove kernels and packages.

On AlmaLinux or Rocky Linux, the steps differ: see how to clear disk space on RHEL-based servers. Ucartz KVM VPS plans include Free Basic Managed Support for small tasks, and for a larger clean-up you can hire an expert.

Official documentation: journald.conf and managing snap updates.

Was this answer helpful?

Still stuck? Open a ticket