How do I remove all unused old kernels in Ubuntu Linux 18.04 LTS using the command line?

Over time you get many Linux kernel packages installed on Ubuntu Linux 16.04 or 18.04 LTS server. All unused Linux kernel takes disk space on Ubuntu server, hence you must delete them from the system. This page shows you how to remove old kernels from an Ubuntu server install.

Ubuntu 18.04 remove kernel that is not used

The procedure to delete all unused old kernels on Ubuntu Linux is as follows:

  1. First, boot into a new kernel
  2. List all other older kernel using the dpkg command
  3. Note down system disk space usage by running the df -H command
  4. Delete all unused old kernels, run: sudo apt –purge autoremove
  5. Verify it

How to list all installed Linux kernel

Simply type the following dpkg command along with egrp command:
sudo dpkg --list | egrep -i --color 'linux-image|linux-headers'

How to list and remove Older Linux Kernel Versions in Ubuntu
Listing old kernel for removal purpose

One can count kernel image that are installed on your system using the wc command:
sudo dpkg --list | egrep -i --color 'linux-image|linux-headers' | wc -l
Sample outputs:


Also note down the amount of disk space available on the file system by typing the following df command:
df -H

Ubuntu list free and used disk space usage
Total 9.3G disk space used on my cloud server

How to remove old kernel versions on Ubuntu 18.04 or above

How to Remove Old Kernels in Ubuntu Linux using apt

Type the following apt command or apt-get command:
sudo apt --purge autoremove
sudo apt-get --purge autoremove

The above commands only work on newer systems as all old Linux kernels and headers automatically flagged as no more needed, and thus can be purged.

Dealing with Ubuntu 16.04 LTS and older

First, get list of currently running Linux kernel, run:
v="$(uname -r | awk -F '-virtual' '{ print $1}')"
echo "$v"

Sample outputs:


Next, I am going to create a list that tells not to delete currently running Linux kernel as follows:
i="linux-headers-virtual|linux-image-virtual|linux-headers-generic-hwe-|linux-image-generic-hwe-|linux-headers-${v}|linux-image-$(uname -r)|linux-image-generic|linux-headers-generic"
echo "$i"

Sample outputs (the following kernel I am going to keep on my systems):


Here is a list of the kernel that needs to be deleted:
dpkg --list | egrep -i 'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i"
Sample outputs:

How to delete old kernels in Ubuntu

Ubuntu 18.04 remove kernel using apt-get command

Finally, I am going to delete all unwated and older Linux kernels on my Ubuntu Linux 16.04 LTS server, run:
$ sudo -i
# apt-get --purge remove $(dpkg --list | egrep -i 'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i")


Again run the df command:
df -H
Sample outputs:

Now I have used 6.7G disk space as compared 9.3G before deleting kernels. In other words, I gained 2G disk free space.


And there you have it. You successfully removed old kernel and header packages from the Ubuntu Linux system. On really older Ubuntu system, we had a command named purge-old-kernels but t is now deprecated. Hence, you must use the apt/apt-get.

Leave a Reply

Your email address will not be published. Required fields are marked *