Easy mode
This method will remove automatically installed packages that no other packages depend on any more. This, of course, includes obsolete kernel versions, with the explicit exception of the currently running kernel, the kernel version that was installed on the system before that and, of course, the latest updated version of the kernel. However, it will also remove any and all other packages that have been marked as installed automatically but have no other packages depending on them. This could lead to unexpected removal of packages. So please check the output of sudo apt-get autoremove
closely.
One-shot, manual method
sudo apt-get autoremove
Automatically remove unused packages
Edit /etc/apt/apt.conf.d/50unattended-upgrades
and un-comment the line Unattended-Upgrade::Remove-Unused-Dependencies "true";
.
Hard mode (for old Ubuntus)
uname -r
prints currently used kernel version. You should not remove the current kernel, linux-image-generic
and linux-image
.
sudo apt-get clean
dpkg --list | grep 'ii linux-image' | awk '{ print $2 }' | sort -V
uname -r
sudo apt-get purge linux-image-x.y.z-generic
sudo apt-get autoremove
Removing multiple kernel versions with one command (long and short version):
sudo apt-get purge linux-image-1.2.4-generic
sudo apt-get purge linux-image-1.2.7-generic
sudo apt-get purge linux-image-1.2.8-generic
sudo apt-get purge linux-image-1.2.9-generic
sudo apt-get purge linux-image-1.2.{4,7..9}-generic
If manually purging old Kernel versions does not work:
If your /boot
partition is full or sudo apt autoremove
isn't cleaning up old kernels, you may need to remove them manually.
This often occurs when a generic kernel metapackage (like linux-image-generic
for General Availability) still depends on older kernel versions, preventing their automatic removal, especially if a newer Hardware Enablement (HWE) kernel (e.g., linux-image-generic-hwe-22.04
) is active.
List all installed kernel images and modules
dpkg -l | grep -E 'linux-image-[0-9]+\.|linux-modules-[0-9]+\.' | grep '^ii'
Identify the old versions you wish to remove. Keep at least one recent, working kernel besides the one you are currently running.
Address Metapackage Conflict (GA vs. HWE)
If you're using an HWE kernel (e.g., a 6.x
kernel on Ubuntu 22.04) but older GA kernels (e.g., 5.15.x
) are not being removed, the GA metapackage might be the cause.
- Check if both
linux-image-generic
(pointing to the old GA line) andlinux-image-generic-hwe-XX.YY
(pointing to your current HWE line) are installed:
dpkg -l | grep linux-image-generic
- If so, and you intend to stay on the HWE track, you can remove the GA metapackage:
sudo apt purge linux-image-generic
Carefully review the packages apt
proposes to remove before proceeding.
Purge Specific Old Kernel Versions
For each old kernel version you identified, remove its image, core modules, and extra modules packages explicitly:
sudo apt purge linux-image-KERNEL_VERSION linux-modules-KERNEL_VERSION linux-modules-extra-KERNEL_VERSION linux-headers-KERNEL_VERSION
Removing all components simultaneously helps prevent apt
from trying to install an unsigned kernel version as a replacement.
After purging the kernel packages, run apt autoremove
to get rid of any orphaned dependencies, such as the corresponding kernel headers
Update your GRUB configuration:
update-grub
Cleanup
If dpkg
warns about non-empty directories in /lib/modules/KERNEL_VERSION
after purging, you can inspect them (e.g., ls -alR /lib/modules/KERNEL_VERSION
) and then manually remove them if they only contain kernel-specific leftovers.
After kernel packages are removed sometimes their configuration files remain on the system. Packages in this state are marked with rc
. Purging these rc
state packages fully completes their uninstallation and is good for system hygiene, although it typically recovers a negligible amount of disk space as the larger kernel image and module files should already be gone.
To see what will be targeted run:
dpkg -l | grep '^rc' | grep -E 'linux-(image|headers|modules|modules-extra)-[0-9]+'
To remove them use:
sudo apt purge $(dpkg -l | grep '^rc' | grep -E 'linux-(image|headers|modules|modules-extra)-[0-9]+' | awk '{print $2}')
Note on Future Distribution Upgrades:
Removing the old GA metapackage is safe for future distribution upgrades.
Ubuntu's do-release-upgrade
tool detects the missing metapackage and automatically installs the correct linux-generic metapackage for the new release, ensuring supported kernel upgrade path.
The system will transition from the old HWE track to the new release's standard GA track.
Compress initramfs
You might still not have enough space in /boot
because even one kernel image alone takes up too much space. Then you can additonally change the compression algorithm for your initramfs
, which makes it compress much smaller. This has an effect on boot time because it takes slightly longer to decompress. Should you do this change because of a do-release-upgrade
(distribution upgrade), then this will likely be changed to another algorithm with the upgrade.
sudo vim /etc/initramfs-tools/initramfs.conf
Change the line with COMPRESS
to the following:
COMPRESS=lzma
Afterwards rebuild your kernel image:
sudo update-initramfs -u -k all
After this it makes sense to do a reboot.
Check size of /boot
To check what takes up the space in /boot
df -h | grep boot
ls -lha /boot
Sources for more information when necessary
- https://askubuntu.com/questions/1429216/very-high-boot-space-requirement-to-go-from-20-04-to-22-04-preventing-upgrade Show archive.org snapshot
- https://askubuntu.com/questions/89710/how-do-i-free-up-more-space-in-boot Show archive.org snapshot
Geordi
We have a tool, called geordi Show archive.org snapshot , which helps you to remove old kernels (available in geordi < 1.7.0). You need to run this command as root.
# rvm
gem install geordi
rvmsudo geordi purge-kernels
# rbenv
sudo gem install geordi
sudo geordi purge-kernels