Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Clean and maintain your Linux system

By updated

Improve this page

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 upOriginImpact
Orphan packagesDependencies of removed softwareTake space, unused
Download cache.deb or .rpm archives kept after installationCan reach several GB
Old kernelsKernel versions kept after an updateAround 200 MB each (see the figures below)
Leftover config filesConfiguration 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:

DistributionKernel modules/boot
Debian 13 (kernel 6.12.116+deb13)108 MB69 MB
Ubuntu 26.04.1 (kernel 7.0.0-43)173 MB71 MB
AlmaLinux 10.2 (kernel 6.12.0-211)106 MB88 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.

Terminal window
sudo apt autoremove

APT 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:

Output of sudo apt autoremove (Ubuntu 26.04.1), answer n
Solving dependencies...
REMOVING:
nginx-common
Summary:
Upgrading: 0, Installing: 0, Removing: 1, Not Upgrading: 1
Freed space: 232 kB
Continue? [Y/n] n
Abort.

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:

Output of sudo apt autoremove on a clean system (Ubuntu 26.04.1)
Solving dependencies...
Summary:
Upgrading: 0, Installing: 0, Removing: 0, Not Upgrading: 1

To also remove the leftover configuration files:

Terminal window
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:

Output of dpkg -l | grep '^rc' (Ubuntu 26.04.1)
rc chrony 4.8-2ubuntu1 amd64 Versatile implementation of the Network Time Protocol

The 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.

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.

Terminal window
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:

Output (Ubuntu 26.04.1, before apt clean)
8.8M /var/cache/apt/archives/

To delete the cache:

Terminal window
sudo apt clean

This 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:

Output of du -sh after apt clean (Ubuntu 26.04.1)
8.0K /var/cache/apt/archives/

For a more conservative cleanup (only removes the packages that can no longer be downloaded from the repositories):

Terminal window
sudo apt autoclean

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:

Terminal window
df -h

The 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):

Output of df -h (Debian 13, excerpt)
Filesystem Size Used Avail Use% Mounted on
udev 874M 0 874M 0% /dev
tmpfs 184M 700K 184M 1% /run
/dev/sda2 20G 963M 19G 5% /
...
tmpfs 919M 72K 919M 1% /tmp

On 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:

Terminal window
sudo du -sh /var/log /var/cache /tmp /home

The 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:

Output of sudo du -sh /var/log /var/cache /tmp /home (Debian 13)
17M /var/log
103M /var/cache
72K /tmp
60K /home

Here, /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:

Terminal window
sudo du -h /var --max-depth=1 | sort -rh | head -10

This command shows /var itself on the first line (its total), then its 9 largest subdirectories, sorted from the biggest to the smallest:

Output (Debian 13, November 13, 2026)
225M /var
106M /var/lib
103M /var/cache
17M /var/log
24K /var/tmp
4.0K /var/spool
4.0K /var/opt
4.0K /var/mail
4.0K /var/local
4.0K /var/backups

The 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:

  1. Check the space before cleaning:

    Terminal window
    df -h /
  2. Refresh and apply the updates:

    Terminal window
    sudo apt update && sudo apt upgrade -y
  3. Remove the orphan packages:

    Terminal window
    sudo apt autoremove -y
  4. Clear the cache:

    Terminal window
    sudo apt clean
  5. Check the space gained:

    Terminal window
    df -h /

For the regulars, the same routine fits on one line:

Terminal window
sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y && sudo apt clean

These 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.

FrequencyContext
Every weekWorkstation
After each lab sessionTest virtual machine
Every month, in a maintenance windowProduction 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.

SymptomLikely causeFix
No space left on devicePartition 100% full, or no free inode leftClear 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 nothingNo orphan packagesSystem already clean, nothing to do
/var/log very largeLogs that keep growingFind the largest files with sudo du -h /var/log before deleting anything, and never erase the whole directory
/tmp takes up a lot of spaceTemporary files piled upCheck 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 spaceFull diskapt 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

  • autoremove removes orphan packages (unneeded dependencies).
  • apt clean / dnf clean all empty the download cache.
  • df -h checks disk space; du -sh identifies the large directories.
  • A full disk blocks updates and can corrupt services.
  • Always read the list of packages that autoremove offers to remove.
  • On Debian and Ubuntu, apt autoremove also removes old kernels, except the running kernel and the newest one; DNF limits their number by itself (installonly_limit=3 on 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

6 questions
6 min
70% to pass

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