How to Remove Old Kernels in Debian and Ubuntu

After installing a new Linux
kernel, old kernels are not automatically deleted. They remain in your disk
(/boot
partition). You have to delete them manually. Why?

The main reason is to save disk space, which occupied by old kernels. Some systems may become unusable if not enough disk
space is available in /boot partition. However, disk space is not a problem in modern systems.

An other reason is to clean up Grub boot menu. In Ubuntu, Grub
boot menu is hidden by default. You can display it by pressing "SHIFT" continuously at boot time.
More details in this post.

You will never delete your current kernel, of course. It is recommended to keep at least one or two
older kernels, so
you can boot your system in an emergency situation (hardware or software compatibility issues with the current
kernel).

After removing old kernels, new grub boot menu will be available in next reboot.

In a healthy Debian system, each time you install a package or update your system, you will be prompted that old
kernels (if any) can
ber removed. You will get the message "The following packages were automatically installed and are no longer
required". This message is no specific for kernel, but for any package which no longer required in your system:

(click for full image)

In this case you may use:

apt-get autoremove

(click for full image)

This would be enough to remove old kernels from your system. But, below, it is described a completed and more safe
approach.

Get current status

First things first. Which is your current kernel?

Use the command

uname -r

(click
the
thumb for full image)

or

uname -mrs

(click
the
thumb for full image)

or

uname -a

(click
the
thumb for full image)

Method A (recommended for Debian users)

Get list of installed kernels (and headers)

dpkg --list | grep linux-image

(click
the
thumb for full image)

Do the same for kernel headers:

dpkg --list | grep linux-headers

(click
the
thumb for full image)

As root, remove old kernels one by one, using:

apt-get --purge remove linux-image-XXX

Finally, as root, update grub:

update-grub2

and reboot:

systemctl reboot

Method B - purge-old-kernels (recommended for Ubuntu users)

purge-old-kernels is a simple bash script, which uses apt remove --purge command to remove
old kernels.

It will be available in your system after installing package
byobu. byobu is a handy terminal multiplexer (more info here). There is no need
to use byobu. Just install it. So:

sudo apt-get install byobu

Note: in older releases you might need to to install bikeshed.

Using man page:

man purge-old-kernels

you get the answer of what purge-old-kernels can do for you.

This program will remove old kernel and header packages from the system, freeing
disk space. It will never remove the currently running kernel. By default, it will
keep at least the latest 2 kernels, but the user can override that value using the
--keep parameter.

Your comments are
welcomed!

This site actively encourages commenting on any post.
Comments are
not pre-moderated, but this community does not tolerate
direct or
indirect attacks, name-calling or insults. Please, read
terms of use
and Comment Policy at privacy
policy.