Introduction

In Linux, the kernel is the core of the operating system. As new versions of Linux are released, your system may update to a newer kernel.

By default, modern Linux versions keep the current kernel, plus one older version. However, in some instances, Linux doesn’t remove old versions of the kernel. One common problem of having old kernels is having an extensive list of bootable kernels on the GRUB (boot) menu.

This guide will help you remove old and unused Linux kernels on your Ubuntu system.

Tutorial on how to remove old or unused kernels on Ubuntu 18.04

Prerequisites

  • A system running Ubuntu 19.04, 18.04, 16.04
  • A terminal window / command line (Ctrl+Alt+T, search > terminal)
  • A user account with sudo privileges

Remove Old Kernel Ubuntu 18.04 & 19.04

Display a List of Kernel Versions

To view a list of all kernel versions installed,  entering the following:

sudo dpkg ––list | egrep –i ––color ‘linux-image|linux-headers’

The output shows a list of all files labeled linux-image or linux-header. Use this to get an approximate number of old kernels on your system.

You can also count the number of kernels with the command:

sudo dpkg ––list | egrep –i ––color ‘linux-image|linux-headers’ | wc –l

This command displays the total number of both linux-image and linux-headers.

Displaying the number of linux images and Linux headers on Ubuntu

Need to find out which Ubuntu Kernel version is running?

Remove All Old Kernels Using the Command Line

The APT package manager can automatically remove all old kernels. To do so, run the command:

sudo apt-get ––purge autoremove

The system scans for unused kernels and displays a summary of the files it wants to delete. It prompts you to confirm your choice to remove old kernels by pressing y and Enter to delete.


Note: If you still haven't had the chance, check out the newest features and improvements from the Linux Kernel 5.0 upgrade.


Manually Remove Individual Kernels from Command-Line

You can manually remove an individual kernel by naming it specifically.

1. First, list the kernels with their installation status:

dpkg -l | tail -n +6 | grep -E 'linux-image-[0-9]+' | grep -Fv $(uname -r)

The first two letters for each kernel indicate its status:

  • rc – already removed
  • ii – installed, eligible for removal
  • iU – downloaded and queued for installation

List kernels with installation status on Linux to find kernels available for removal.

2. Remove a kernel with the ii status:

sudo dpkg ––purge [kernel_version]

Make sure you type the exact name and number of the kernel you want to remove. For example:

sudo dpkg ––purge linux-image-5.3.0-28-generic

How to Delete Old Unused Kernels in Ubuntu 16.04 and Older

By default, previous Linux versions didn’t include tools to manage old kernels.

1. To remove old Linux kernels, you first need to install the byobu tool:

sudo apt-get install byobu

2. Then, run the utility by entering the following:

sudo purge-old-kernels

The utility scans your system and removes all but the latest two kernels. If you want to keep more than the default two, use the ––keep option:

sudo purge-old-kernels ––keep 3 –qy

For more information on the byobu tool, enter the following:

man purge-old-kernels

Note: Do not remove the current Linux kernel in use. Doing so could render your system unable to boot.  Use the uname -r command if you need your current kernel version.


Remove Old Kernels Using Ubuntu Software Center

In older versions of Ubuntu, you can remove kernels manually using the Software Center. This option only works if you’re running a graphical interface (GUI).

1. Open the Ubuntu Software Center.

2. Enable the search bar and search for linux-image.

3. In the lower-left corner, you should see a hyperlink for Show xx technical items. Click this link to display the list of Linux kernels.

4. Refer back to your current kernel, which needs to remain on the system. Select any of the older versions and click the Remove button.

5. Refresh the GRUB (boot) menu by entering the following into a terminal window:

sudo update-grub

Note: Leave at least one previous kernel on your system. Your system was proven to be stable with that kernel. If you have difficulties after a software update, you can revert to the previous (working) Linux kernel.


Conclusion

You should now know several methods to remove old kernels from Ubuntu 19.04, 18.04, & 16.04 or older. Purging kernels can help you recover wasted disk space. It’s also considered a best practice for good system hygiene, to prune out old files and dependencies.


Next you should also read