To free disk space on Linux, three commands are enough:
remove unneeded packages (autoremove), clear the cache
(clean) and check the space left (df -h). This guide
shows you how to keep your system clean and healthy. It builds on the steps
covered in updating your Linux system.
What you will learn
- Remove the orphan packages that are no longer needed
- Clear the cache of downloaded packages
- Check the available disk space
- Identify the largest directories
- Apply a regular maintenance routine
Why clean your system?
Over time, your system piles up useless items:
| What piles up | Origin | Impact |
|---|---|---|
| Orphan packages | Dependencies of removed software | Take space, unused |
| Download cache | .deb or .rpm archives kept after installation | Can reach several GB |
| Old kernels | Kernel versions kept after an update | Around 200 MB each (see the figures below) |
| Leftover config files | Configuration of a package removed with remove (not purge) | Negligible but cluttering |
On a server with a limited disk, or a lab virtual machine, running out of space can block updates and stop the system from working properly.
The kernel is the central program of Linux, the one that drives the hardware. Each kernel update installs a new one and leaves the old one in place, so that you can reboot into it if the new one causes a problem. These old kernels are what piles up.
They almost remove themselves. On Debian and Ubuntu, apt autoremove keeps by
default two kernels: the one running right now (the uname -r command
shows it) and the newest one. If the running kernel is already the newest, it
keeps the second newest instead. It removes all the others. On RHEL and
derivatives, DNF limits the number of kernels kept:
grep installonly_limit /etc/dnf/dnf.conf answers installonly_limit=3 on
AlmaLinux 10.2, that is three kernels kept.
A kernel weighs in the order of a hundred MB for its modules, plus its files in
/boot. Measured on November 13, 2026, with
sudo du -sh /usr/lib/modules/* /boot on systems that had only one kernel
installed:
| Distribution | Kernel modules | /boot |
|---|---|---|
| Debian 13 (kernel 6.12.116+deb13) | 108 MB | 69 MB |
| Ubuntu 26.04.1 (kernel 7.0.0-43) | 173 MB | 71 MB |
| AlmaLinux 10.2 (kernel 6.12.0-211) | 106 MB | 88 MB |
The /boot figure in this table includes the small EFI system partition
mounted on /boot/efi, and counts only one kernel: each additional kernel adds
its own share.
How do you remove unneeded packages?
When you remove a piece of software, its dependencies often stay
installed. The autoremove command detects and removes these orphan
packages.
sudo apt autoremoveAPT displays the list of orphan packages and a numeric summary, then asks for
confirmation before removing them. Read that list carefully: it is the moment
to spot a package you wanted to keep. Tested on Ubuntu 26.04.1 (APT 3.2.1,
English locale) on November 13, 2026, after apt install nginx followed by
apt remove nginx: nginx-common, installed with nginx, stays behind. Answering
n at the prompt aborts the operation:
Solving dependencies...REMOVING: nginx-common
Summary: Upgrading: 0, Installing: 0, Removing: 1, Not Upgrading: 1 Freed space: 232 kB
Continue? [Y/n] nAbort.The headings are the English ones: REMOVING: lists the packages that are going to
disappear. On a system set to another language, they may be translated.
On a clean system, the command has nothing to remove, which the Summary line
shows with Removing: 0:
Solving dependencies...Summary: Upgrading: 0, Installing: 0, Removing: 0, Not Upgrading: 1To also remove the leftover configuration files:
sudo apt purge $(dpkg -l | grep '^rc' | awk '{print $2}')In the Ubuntu 26.04.1 example, the dpkg -l | grep '^rc' inventory contained one package:
rc chrony 4.8-2ubuntu1 amd64 Versatile implementation of the Network Time ProtocolThe filter of this command deserves an explanation. In the output of
dpkg -l, each line starts with a two-letter status: rc means package
removed (removed) but configuration still present (config), the state in
which a plain apt remove leaves packages that have configuration files. These
leftovers weigh almost nothing, but they clutter the inventory and can be used
again if you reinstall the package later.
Before purging, first list the packages concerned with
dpkg -l | grep '^rc' alone: you will see what is about to disappear.
In this example, the purge succeeded and the rc list was empty afterwards.
Without an interactive terminal, debconf wrote some warnings on its interface
along the way, with no consequence. When no package is in the rc state, the
$(…) command is empty: run with -y, as in this test, apt purge with no
package raises no error and returns code 0, only reminding you that an unneeded
package can be removed by apt autoremove.
sudo dnf autoremoveDNF identifies the packages installed as dependencies that are no longer
needed and offers to remove them. On AlmaLinux 10.2 (DNF 4.20.0), the same
scenario gives a different result from APT: dnf remove nginx itself removes
the dependencies that have become useless (the clean_requirements_on_remove
setting, enabled by default), and dnf autoremove then has nothing left to do
(tested on November 13, 2026). A dependency installed by hand is not removed
by this setting.
Removed: almalinux-logos-httpd-100.4-1.el10_2.noarch nginx-2:1.26.3-6.el10_2.7.x86_64 nginx-core-2:1.26.3-6.el10_2.7.x86_64 nginx-filesystem-2:1.26.3-6.el10_2.7.noarch
Complete!Last metadata expiration check: 0:07:16 ago on Fri 13 Nov 2026 07:10:48 PM UTC.Dependencies resolved.Nothing to do.Complete!How do you clear the package cache?
Every downloaded package is kept in a cache on your disk. This cache lets you reinstall a package without downloading it again, but it grows over time.
To see the size of the cache, run the following command. The output fits on one
line: the size, then the directory measured. Use sudo: the partial
subdirectory is not readable by an ordinary user, and du writes that to the
error output before giving its total.
sudo du -sh /var/cache/apt/archives/On Ubuntu 26.04.1, after a few installations, the cache contained .deb
archives (for example apparmor_5.0.2-0ubuntu1~26.04.1_amd64.deb) and weighed
8.8 MB; no docker-clean file was present in /etc/apt/apt.conf.d/, so APT
did keep the archives by default:
8.8M /var/cache/apt/archives/To delete the cache:
sudo apt cleanThis command empties the whole cache. After an apt clean, packages
will have to be downloaded again if you reinstall them. On the same VM, the
directory dropped to a few KB:
8.0K /var/cache/apt/archives/For a more conservative cleanup (only removes the packages that can no longer be downloaded from the repositories):
sudo apt autocleanTo see the size of the cache (the command may need sudo, depending on the
rights on the subdirectories):
du -sh /var/cache/dnf/On AlmaLinux 10.2, this directory weighed 72 MB on November 13, 2026. It mostly
contains the repository metadata (appstream.solv and baseos.solv files and
their directories), not packages: grep -r keepcache /etc/dnf/ found nothing,
so the default value applies and DNF does not keep packages after a successful
installation.
72M /var/cache/dnf/To delete the cache:
sudo dnf clean allThis command deletes the cached metadata and packages. The metadata is
downloaded again at the next dnf call, which takes time on a slow network.
How do you check disk space?
Before and after cleaning, check the available space. Two commands
answer two different questions: df for each partition, du for
each directory.
An overview with df
The df (disk free) command shows the available space on
each partition:
df -hThe output has one line per mounted filesystem. Read it column by column: the
total size, the space used, the space available, the percentage used, then the
mount point. The headers are in English (Filesystem, Size, Used, Avail)
in the English locale; a system set to French may translate them. Only the
lines of real disks matter to you: / first, then /home or /var if they
have their own partition. The tmpfs lines describe RAM, not the disk.
Measured on Debian 13, on November 13, 2026, on a one-day-old VM (excerpt of
the first four lines and the /tmp line):
Filesystem Size Used Avail Use% Mounted onudev 874M 0 874M 0% /devtmpfs 184M 700K 184M 1% /run/dev/sda2 20G 963M 19G 5% /...tmpfs 919M 72K 919M 1% /tmpOn this machine, the system disk is /dev/sda2 mounted on /, at 5%
use, and /tmp is a tmpfs. On AlmaLinux 10.2, the first line is also
/dev/sda2 (7%), but /tmp does not appear: it is part of /.
The -h option shows readable sizes (GB, MB instead of bytes).
Find the largest directories with du
The du (disk usage) command measures what each directory uses up. The
-s option gives a single total per argument, and -h readable sizes:
sudo du -sh /var/log /var/cache /tmp /homeThe output gives one line per directory requested: the size, then the path. Compare these sizes: the largest directory is the one to look at first. Measured on Debian 13, on November 13, 2026:
17M /var/log103M /var/cache72K /tmp60K /homeHere, /var/cache is the largest of the four, which takes you back to the
package cache seen above.
To identify the largest directories inside a directory:
sudo du -h /var --max-depth=1 | sort -rh | head -10This command shows /var itself on the first line (its total), then
its 9 largest subdirectories, sorted from the biggest to the smallest:
225M /var106M /var/lib103M /var/cache17M /var/log24K /var/tmp4.0K /var/spool4.0K /var/opt4.0K /var/mail4.0K /var/local4.0K /var/backupsThe total comes first because it is the sum of everything that follows, so the
largest value of the sort. Here, /var/lib and /var/cache each weigh about
half of /var.
Run it again on the largest subdirectory to go down one level.
If a write fails with No space left on device while df -h shows free space,
running out of inodes is another possible cause:
control disk space and inodes
explains how to check it.
Which maintenance routine should you follow?
Here is a full cleaning sequence to run regularly:
-
Check the space before cleaning:
Terminal window df -h / -
Refresh and apply the updates:
Terminal window sudo apt update && sudo apt upgrade -y -
Remove the orphan packages:
Terminal window sudo apt autoremove -y -
Clear the cache:
Terminal window sudo apt clean -
Check the space gained:
Terminal window df -h /
For the regulars, the same routine fits on one line:
sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y && sudo apt clean-
Check the space before cleaning:
Terminal window df -h / -
Apply the updates:
Terminal window sudo dnf upgrade -y -
Remove the orphan packages:
Terminal window sudo dnf autoremove -y -
Clear the cache:
Terminal window sudo dnf clean all -
Check the space gained:
Terminal window df -h /
The same routine on one line:
sudo dnf upgrade -y && sudo dnf autoremove -y && sudo dnf clean allThese frequencies concern cleanup only (the autoremove and clean steps).
They are guidelines to adapt to how the machine is used, an editorial opinion
and not a rule set by APT or DNF. The pace of the routine's updates follows the
one in the guide on updates.
| Frequency | Context |
|---|---|
| Every week | Workstation |
| After each lab session | Test virtual machine |
| Every month, in a maintenance window | Production server |
This routine, with its -y options, suits a workstation or a lab. On a
production server, remove -y to read each list of packages, and run the
routine in a maintenance window, as for updates. The guide
manage packages with APT
details APT's cleanup options.
Troubleshooting
What these situations have in common is that they show up somewhere else than
where they are fixed: an update that refuses to install rarely names the
full disk as the culprit. When an operation fails for no
apparent reason, run df -h before any other hypothesis, then follow the
matching line below.
| Symptom | Likely cause | Fix |
|---|---|---|
No space left on device | Partition 100% full, or no free inode left | Clear the cache, remove old kernels with apt autoremove on Debian and Ubuntu (DNF already keeps three kernels, installonly_limit=3); if df -h shows free space, check the inodes (see the section on du) |
apt autoremove removes nothing | No orphan packages | System already clean, nothing to do |
/var/log very large | Logs that keep growing | Find the largest files with sudo du -h /var/log before deleting anything, and never erase the whole directory |
/tmp takes up a lot of space | Temporary files piled up | Check with findmnt /tmp whether /tmp is in memory or on the disk, then delete the files by hand if needed (details after the table) |
| Update blocked by lack of space | Full disk | apt clean or dnf clean all first to free the cache |
For the /tmp line, everything depends on the distribution. On Debian 13, findmnt /tmp shows a tmpfs filesystem: /tmp is in memory and is emptied when the machine shuts down. This also holds for a Debian 12 upgraded to 13, unless /tmp has its own line in /etc/fstab: that partition wins and the tmpfs is not activated. Otherwise the old /tmp files stay on the disk, hidden by the new mount (the Debian 13 Trixie guide, in French, shows how to get them back). A freshly installed Ubuntu 26.04 has the same tmpfs, whereas Ubuntu 24.04 keeps /tmp on the disk and empties it at every boot. On a 24.04 upgraded to 26.04, run findmnt /tmp to see where /tmp is.
On AlmaLinux 10.2, /tmp is a directory on the disk: findmnt /tmp returns no line and tmp.mount is disabled. The q /tmp 1777 root root 10d rule in /usr/lib/tmpfiles.d/tmp.conf makes systemd-tmpfiles delete files older than 10 days during its cleanup. To free the space faster, delete the files by hand.
Key points
autoremoveremoves orphan packages (unneeded dependencies).apt clean/dnf clean allempty the download cache.df -hchecks disk space;du -shidentifies the large directories.- A full disk blocks updates and can corrupt services.
- Always read the list of packages that
autoremoveoffers to remove. - On Debian and Ubuntu,
apt autoremovealso removes old kernels, except the running kernel and the newest one; DNF limits their number by itself (installonly_limit=3on AlmaLinux 10.2). - Adopt a maintenance routine: update → upgrade → autoremove → clean.
- Around 90% use of
/, act without waiting.
Knowledge check
Check that the essentials of this guide stuck. The questions cover only what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Networking, services and logs on Linux
A new full quiz with random questions
Work again only on the questions you missed