Introduction / Why This Is Needed
Each Linux kernel update in Ubuntu installs new linux-image and linux-headers packages. The old versions remain on the disk, occupying 100 to 300 MB each. Over time, they can accumulate gigabytes of "junk" in the /boot partition, leading to "No space left on device" errors when attempting to install updates.
This guide will show you how to safely find and remove obsolete kernel versions, freeing up space while retaining the ability to roll back to a previous stable version.
Prerequisites / Preparation
- Administrator privileges: All commands require
sudo. - Current kernel: You must know exactly which kernel is currently running. Deleting the active kernel will render the system unusable.
- Backup (recommended): Before performing mass package operations, it's wise to create a system snapshot (if you use Timeshift) or at least generate a list of packages to be removed.
- Internet: Not required, but may be needed to reinstall the kernel in case of an error.
Step 1: Identify the Current Active Kernel
First, find out which kernel version is currently in use. Open a terminal (Ctrl+Alt+T) and run:
uname -r
The output will look something like: 5.15.0-86-generic. Memorize or write down this version. This kernel must not be deleted.
You can also view the list of all kernels available for booting in the GRUB menu at system startup.
Step 2: View the List of All Installed Kernels
Now let's see which specific kernel and header packages are installed in the system. This will give you the full picture.
dpkg -l 'linux-image-*' | grep ^ii
Or a more readable variant:
apt list --installed 2>/dev/null | grep -E 'linux-image|linux-headers'
In the output, you will see a list of packages, for example:
linux-image-5.15.0-86-genericlinux-image-5.15.0-85-genericlinux-headers-5.15.0-86linux-headers-5.15.0-85
Important: Packages related to your current kernel (from Step 1), and typically one previous one (the newest of the old versions) should be kept for rollback. Delete versions that are clearly obsolete (e.g., 5.15.0-50, 5.15.0-60, etc., if you are already on 86).
Step 3: Safe Automatic Removal via autoremove
The safest and simplest method is to let the system automatically identify unnecessary packages. APT marks packages as "automatically installed" and "no longer needed" when newer versions replace older ones.
Run the command:
sudo apt-get autoremove --purge
What it will do:
autoremove— removes packages that are no longer needed as dependencies (including oldlinux-image-*andlinux-headers-*packages if they were automatically installed).--purge— also removes the configuration files of these packages, providing maximum space savings.
Carefully review the list of packages that apt will propose to delete! Ensure that none of them match your current kernel version (uname -r). If the current kernel appears in the list — cancel the operation (Ctrl+C) and proceed to Step 4 for manual removal.
Step 4: Manual Removal of Specific Old Kernels (If Needed)
If autoremove missed some old kernels (e.g., they were installed manually), remove them manually.
- Compile a list of versions to remove. For example, you decide to remove kernels
5.15.0-50and5.15.0-55. - For each version, execute the purge command:
sudo apt-get purge linux-image-5.15.0-50-generic linux-headers-5.15.0-50 linux-headers-5.15.0-50-generic
sudo apt-get purge linux-image-5.15.0-55-generic linux-headers-5.15.0-55 linux-headers-5.15.0-55-generic
Why purge? A regular remove leaves configs behind, while purge removes everything. For kernels, this is important because configuration files in /boot can occupy dozens of megabytes.
Tip: You can remove multiple versions in a single command by listing them separated by spaces.
Step 5: Update the GRUB Bootloader Configuration
After removing the packages, the kernel files are physically deleted from /boot, but the GRUB bootloader menu may still contain references to them. Update its configuration:
sudo update-grub
This command will scan /boot and generate a new menu containing only the currently installed kernels.
Verification
- Check free space on
/boot:df -h /boot
You should see a significant increase in free space (e.g., from 10% to 40-50%). - Check the list of installed kernels (as in Step 2). Obsolete versions should be gone.
- Reboot the system (
sudo reboot) and ensure it boots normally with the remaining kernel. At startup, the GRUB menu should show only relevant versions.
Potential Issues
Issue: apt-get autoremove tries to delete the current kernel
Cause: The system incorrectly marked packages, or you accidentally installed a new kernel but haven't rebooted yet. Solution:
- Cancel the operation.
- Reboot into the new kernel (
uname -rwill show you are on it). - Run
sudo apt-get autoremoveagain. Now the old kernels will be correctly identified as unnecessary.
Issue: After removing kernels, update-grub doesn't find new kernels
Cause: You may have accidentally deleted all kernels except one, or header packages (linux-headers) are left in an inconsistent state.
Solution:
- Check that
/bootcontains files likevmlinuz-<version>andinitrd.img-<version>. - Reinstall the latest stable kernel:
sudo apt-get install linux-generic. - Run
sudo update-grubagain.
Issue: Not enough space even for autoremove
Cause: The /boot partition is 100% full, and apt cannot create temporary files to process packages.
Solution:
- Temporary workaround: Manually delete the oldest kernel files from
/boot(e.g.,vmlinuz-5.15.0-50-generic,initrd.img-5.15.0-50-generic,config-5.15.0-50,System.map-5.15.0-50). Do this carefully, knowing exactly what you delete! - After freeing up 50-100 MB, run
sudo apt-get -f installand thensudo apt-get autoremove --purgeto complete the process correctly.
Issue: System fails to boot after kernel removal
Cause: The only working kernel was deleted or GRUB is corrupted. Solution:
- Boot into Recovery Mode from the GRUB menu (option "Advanced options for Ubuntu").
- Select the
fsckoption to check the filesystem, thendpkgto repair packages. - If that doesn't work, boot from a LiveCD, mount the root partition, and reinstall GRUB (
chrootinto the system, then rungrub-installandupdate-grub). - As a last resort, restore the deleted kernel files from a backup or reinstall the kernel using
apt-get install.