MRW

Remove All Old Kernel Images in Ubuntu

The Problem

Unfortunately sudo apt-get --purge autoremove does not remove old unused kenel images. This often results in a full /boot partition, especially on systems with small /boot and encrypted root /.

The Solution

It is simple to remove all old images, but keeping the running one. For your comfort and safety, please upgrade your system, then reboot before you execute this command to make sure, the latest kernel is installed and running.

Remove all but the running kernel images:

sudo apt-get remove --purge 'linux-image-[0-9].*' linux-image-$(uname -r)+

Please check the list of images to be removed before you accept and make sure at least one image is installed before you reboot.

You can see a list of all existing packages whose name start with linux-image using the command dpkg -l 'linux-image*'. The packages with ii in front are installed.

If you have installed headers too, you can repeat the command by replacing image with header:

sudo apt-get remove --purge 'linux-headers-[0-9].*' linux-headers-$(uname -r)+

If you get an error, make sure you are running the current kernel version, i.e. upgrade then reboot.

Background Knowledge

The plus (+) sign behind a package name inverts the meaning and installs the package instead of uninstalling it, so here, the plus keeps the running kernel version. The uname command is there to find your actual kernel package version and type (here 3.2.0-32-generic). Actually on my system here, the uname commands would expand the above line to:

sudo apt-get remove --purge 'linux-image-.*' linux-image-3.2.0-32-generic+