Introduction / Why This Is Needed
After system updates in Linux, many old kernel versions accumulate. Each kernel occupies 100–300 MB on the /boot system partition. Over time, this can fill up the partition, causing errors during update installation or even preventing the system from booting. Removing unnecessary kernels:
- Frees up disk space.
- Reduces security risks, as old kernels may contain vulnerabilities.
- Simplifies the bootloader menu, leaving only relevant options.
In this guide, you will learn how to safely remove old kernels in Debian/Ubuntu-based distributions (and get general recommendations for other systems).
Requirements / Preparation
Before you begin, ensure that:
- You have terminal access with sudo privileges (or root).
- You know the current kernel version (see step 2) to avoid removing it.
- It is recommended to leave at least one previous kernel in case the new kernel causes hardware or driver issues.
Step-by-Step Instructions
Step 1: Check Installed Kernels
First, review the list of all installed kernels. For Debian/Ubuntu-based distributions, run:
dpkg --list | grep linux-image
Example output:
ii linux-image-5.4.0-100-generic 5.4.0-100.112 amd64 Signed kernel image generic
ii linux-image-5.4.0-99-generic 5.4.0-99.111 amd64 Signed kernel image generic
For Fedora/CentOS/RHEL, use:
rpm -qa | grep kernel
Note the kernel versions (e.g., 5.4.0-100-generic). These are the packages that can be removed.
Step 2: Identify the Current Kernel
To avoid removing the active kernel, find its version:
uname -r
Example output:
5.4.0-100-generic
Write down this version. You must not remove the package with this version, or the system will not boot.
You can also check which kernels are available in the GRUB menu, but this is optional.
Step 3: Remove Old Kernels
Method A: Using apt autoremove (Ubuntu/Debian)
The apt autoremove command removes packages installed as dependencies that are no longer needed. It sometimes removes old kernels:
sudo apt autoremove --purge
The --purge flag also removes configuration files. After running, check if old kernels remain (step 1). If they do, proceed to Method B.
Method B: Manual Removal via dpkg/apt purge
Remove specific packages of old kernels and their corresponding headers. For example, for version 5.4.0-99:
sudo apt-get purge linux-image-5.4.0-99-generic linux-headers-5.4.0-99-generic
Important: Do not remove packages for the current kernel (from step 2). It is also recommended to keep at least one backup kernel (e.g., the previous version).
If you need to remove all kernels except the current one, you can use this script (for Debian/Ubuntu):
#!/bin/bash
# Get the current kernel version (without architecture)
current_kernel=$(uname -r | grep -oE '^[0-9]+\.[0-9]+\.[0-9]+-[0-9]+')
# Get a list of installed kernel versions (from linux-image packages)
installed_kernels=$(dpkg --list | grep linux-image | awk '{print $2}' | grep -oE '[0-9]+\.[0-9]+\.[0-9]+-[0-9]+')
# Remove all except the current one
for kernel in $installed_kernels; do
if [ "$kernel" != "$current_kernel" ]; then
sudo apt-get purge -y "linux-image-$kernel-generic" "linux-headers-$kernel-generic"
fi
done
For other distributions:
- Fedora:
sudo dnf remove kernel-version - Arch Linux:
sudo pacman -R linux-version
Step 4: Update Bootloader Configuration
After removing kernels, update the GRUB configuration so the boot menu does not contain entries for removed kernels:
sudo update-grub
For systems using systemd-boot, an update may not be necessary, but check the configuration in /boot/loader/entries/.
Step 5: Reboot the System
Reboot the computer to ensure the system boots with the current kernel:
sudo reboot
After rebooting, verify that the system is functioning correctly (network, graphical interface, etc.).
Verification
- Freed space: check how much space the
/bootpartition now occupies:df -h /boot
You should see a significant increase in free space. - Kernel list: confirm that old kernels are removed:
dpkg --list | grep linux-image # Debian/Ubuntu rpm -qa | grep kernel # Fedora/CentOS - Current kernel: verify that the active kernel has not changed:
uname -r - Bootloader menu: on the next reboot, ensure that removed kernel versions no longer appear in the GRUB menu.
Potential Issues
Issue: Accidentally Removing the Current Kernel
Symptom: The system fails to boot, showing grub rescue> or no such device errors.
Solution:
- Reboot the computer and select a previous kernel in the GRUB menu (if available).
- Boot with it and reinstall the required kernel, for example:
sudo apt install linux-image-5.4.0-100-generic - Update GRUB (
sudo update-grub) and reboot.
If no previous kernel exists, you will need to boot from a LiveCD, mount the root filesystem, and use chroot to recover.
Issue: GRUB Does Not Update
Symptom: After removing kernels, the boot menu still shows entries for removed kernels.
Solution:
- Ensure you ran
sudo update-grub. - Check that the
grub-commonpackage is installed. - In rare cases, you may need to reinstall GRUB:
sudo apt install --reinstall grub-pc sudo update-grub
Issue: Dependency Errors During Removal
Symptom: The removal command fails with unsatisfied dependency errors.
Solution:
- Remove kernels along with their headers and modules, as shown above.
- After manual removal, run
sudo apt autoremove --purgeto clean up remaining dependencies. - If the problem persists, use
sudo apt-get -f installto fix broken packages.
Issue: /boot Partition Fills Up Again
Symptom: Some time after cleanup, the /boot partition fills up again.
Solution:
- This is normal: new kernel updates will be installed. Schedule regular checks (e.g., monthly).
- For automation, you can use tools like
byobu(with thepurge-old-kernelsoption), but be cautious—configure them to keep at least one backup kernel.
Issue: Removing Kernels in Non-Standard Configurations
If you use custom kernels or non-standard paths, ensure you are removing the correct packages. Instead of dpkg, you could manually delete files from /boot, but this is highly discouraged—it's better to use the package manager.