Ubuntu 18.04 remove all unused old kernels

How do I remove all unused old kernels in Ubuntu Linux 18.04/20.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. The following commands works with Ubuntu 18.04 and 20.04 LTS server or desktop systems.

ADVERTISEMENTS

Ubuntu 18.04 remove kernel that is not used

The procedure to delete all unused old kernels on Ubuntu Linux version 18.04 and 20.04 LTS 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:

57

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

Type the following apt command or apt-get command:
sudo apt --purge autoremove
OR
sudo apt-get --purge autoremove
How to Remove Old Kernels in Ubuntu Linux using apt
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:

4.15.0-65-generic

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):

linux-headers-virtual|linux-image-virtual|linux-headers-generic-hwe-|linux-image-generic-hwe-|linux-headers-4.15.0-65-generic|linux-image-4.15.0-65-generic|linux-image-generic|linux-headers-generic

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:

linux-headers-4.15.0-45
linux-headers-4.15.0-45-generic
linux-headers-4.15.0-64
linux-headers-4.15.0-64-generic
linux-headers-4.15.0-65
linux-headers-4.4.0-165
linux-headers-4.4.0-165-generic
linux-image-4.10.0-27-generic
linux-image-4.10.0-33-generic
linux-image-4.10.0-35-generic
linux-image-4.10.0-37-generic
linux-image-4.10.0-38-generic
linux-image-4.10.0-40-generic
linux-image-4.10.0-42-generic
linux-image-4.13.0-26-generic
linux-image-4.13.0-31-generic
linux-image-4.13.0-36-generic
linux-image-4.13.0-37-generic
linux-image-4.13.0-38-generic
linux-image-4.13.0-39-generic
linux-image-4.13.0-41-generic
linux-image-4.13.0-43-generic
linux-image-4.13.0-45-generic
linux-image-4.15.0-30-generic
linux-image-4.15.0-32-generic
linux-image-4.15.0-33-generic
linux-image-4.15.0-34-generic
linux-image-4.15.0-36-generic
linux-image-4.15.0-38-generic
linux-image-4.15.0-39-generic
linux-image-4.15.0-42-generic
linux-image-4.15.0-43-generic
linux-image-4.15.0-45-generic
linux-image-4.15.0-64-generic
linux-image-4.4.0-165-generic

How to delete old kernels in Ubuntu

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")

Ubuntu 18.04 remove kernel using apt-get command

Result

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

Filesystem      Size  Used Avail Use% Mounted on
udev            1.1G     0  1.1G   0% /dev
tmpfs           210M  6.0M  204M   3% /run
/dev/vda1        42G  6.7G   35G  17% /
tmpfs           1.1G     0  1.1G   0% /dev/shm
tmpfs           5.3M     0  5.3M   0% /run/lock
tmpfs           1.1G     0  1.1G   0% /sys/fs/cgroup
tmpfs           103k     0  103k   0% /var/lib/lxd/shmounts
tmpfs           103k     0  103k   0% /var/lib/lxd/devlxd
tmpfs           210M     0  210M   0% /run/user/0

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

Conclusion

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.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
3 comments… add one
  • Anonymous Sep 21, 2020 @ 16:43

    I tested this on a system running Ubuntu 20.04, 19.04, 18.04, and 16.04. It worked on all of them. Why they are not removing kernels by default?

  • Gene Cooperman Oct 27, 2020 @ 17:25

    You wrote ‘sudo apt-get --purge ...‘. I think you meant ‘sudo apt-get purge ...

    • 🐧 Vivek Gite Oct 28, 2020 @ 5:02

      No syntax is correct. It is is equivalent to the purge command for autoremove option. The apt-get accepts both syntax:

      sudo apt-get --purge autoremoe # valid for 'autoremove'
      sudo apt-get purge autoremoe # this will not work for 'autoremove'
      sudo apt-get purge pkg1 # valid for remvoing 'pkg1' including removal of config files 
      

      Hope this helps. See man page:

      man apt-get

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.