remove unused kernels in ubuntu

How to Remove Unused Kernels in Ubuntu

Every time Ubuntu updates its kernel, it keeps aside the old kernel in a separate file on your system, without deleting it. This is useful in case your update fails, allowing you to switch back to the old kernel. But over time, it results in many old and unused kernel files lying idle on your system, and occupying unnecessary disk space. It is advisable to remove all old and unused kernels except the current running one and the last one. In this article, we will look at how to remove unused kernels in Ubuntu. They work in most Ubuntu/Debian systems.


How to Remove Unused Kernels in Ubuntu

Here are the steps to remove unused kernels in Ubuntu.


1. List all kernels

Open terminal and run the following command to list all versions of kernels on your system.

$ dpkg --list | egrep -i --color 'linux-image|linux-headers'

# only installed #
$ dpkg --list | grep -i -E --color 'linux-image|linux-kernel' | grep '^ii'

The first command will list all kernel files on your system. The second lists only the installed ones.

If you want to count these kernels then pass their output to wc -l command as shown below.

$ dpkg --list | egrep -i --color 'linux-image|linux-headers' | wc -l

# only count installed using wc #
$ dpkg --list | egrep -i --color 'linux-image|linux-headers' | grep '^ii' | wc -l


2. Remove unused kernels

Run the following command to remove unused or old kernels in Ubuntu 16+

$ sudo apt --purge autoremove
OR
$ sudo apt-get --purge autoremove

In Ubuntu 16+, all old kernels are automatically flagged as unnecessary and automatically removed when we run the above commands.

In case of Ubuntu 16.04 LTS and older versions, we need to manually get a list of all old kernels and then delete them.

First, we use df -H command to determine the amount of free space on disk.

$ df -H

Next we get a list of all such old and unused kernels in Ubuntu. First get version of current running kernel with the following command.

$ v="$(uname -r | awk -F '-virtual' '{ print $1}')"
$ echo "$v"
4.15.0-65-generic

Next, we obtain the string for kernel name using the above variable $v.

i="linux-headers-${v}"
$ echo $i
linux-headers-4.15.0-65-generic

If you want to exclude certain kernels from deletion, you can simply concatenate them to $v variable. Here is an example to exclude kernels starting with linux-headers-virtual along with the latest version. We use | operator to imply ‘or’ operation.

i="linux-headers-virtual|linux-headers-${v}"
echo "$i"

Here is the command to list all kernels starting with linux-image or linux-generic, except the one mentioned above.

dpkg --list | egrep -i 'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i"

Here is the command to delete all kernels present in above list. We basically pass it to ‘apt-get –purge remove’ command.

# apt-get --purge remove $(dpkg --list | egrep -i 'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i")

Alternatively, if the above command doesn’t work for you, you can also use the following commands to list and delete kernels starting with linux-image or linux-generic, except the mentioned earlier.

# dpkg --list | egrep -i 'linux-image|linux-headers' | awk -F " " '/ii/{ print $2}' | egrep -v linux-image-"$v"

# apt-get –purge remove $(dpkg --list | egrep -i 'linux-image|linux-headers' | awk -F " " '/ii/{ print $2}' | egrep -v -e "linux-image-"$v"" -v -e "linux-headers-"$v"")

That’s it. You can use df -H command to determine free space after deletion of kernels.

It is advisable to remove old or unused kernels from your system on a regular basis. In fact, you can even create a cronjob with the above commands and run them once in 6 months, to clear all old kernel versions.

Also read:

How to Remove Unused/Old Kernels from RHEL/CentOS/Fedora
How to Install Sublime Text in Linux
HAProxy Load Balancer Configuration in Linux
How to Clone Partition or Hard Disk in Linux
How to Assign Multiple IP Addresses to Network Interface

2 thoughts on “How to Remove Unused Kernels in Ubuntu

  1. Just a little adjustment:

    #!/bin/sh
    v=”$(uname -r | awk -F ‘-virtual’ ‘{ print $1}’)”
    echo “$v”
    i=”linux-headers-${v}”
    echo $i
    dpkg –list | egrep -i ‘linux-image|linux-headers’ | awk -F ” ” ‘/ii/{ print $2}’ | egrep -v linux-image-“$v”
    apt-get –purge remove $(dpkg –list | egrep -i ‘linux-image|linux-headers’ | awk -F ” ” ‘/ii/{ print $2}’ | egrep -v -e “linux-image-“$v”” -v -e “linux-headers-“$v””)

Leave a Reply

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