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.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Ubuntu 18.04 LTS
Est. reading time 4 minutes

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:


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


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

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


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.

Removing old kernels using GUI software updater tool

Ubuntu comes with graphical management of software packages updates called update-manager. It is a frontend for the apt command package management system. Start update manger from GUI or open the terminal app, then type:
Make sure you select an option called “Unused kernel updates to be removed” > click on the Install Now button:
Removing old and unused Linux kernels on your Ubuntu system using GUI
Naturally, the GUI method works on the Ubuntu desktop. Server users need to stick with the CLI method.


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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 7 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig 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
7 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
  • Paul Feb 21, 2021 @ 18:12

    Thank you for this. Stupid question time. It says I have 26 different kernels, but then doesn’t delete any of them. Am I doing something wrong of does the new kernel have dependencies in the old kernels?

    • 🐧 Vivek Gite Feb 21, 2021 @ 18:51

      No, but you might have config files left from older kernel that is no longer needed for the current Linux kernel. So delete them.

      • Jus Mar 20, 2021 @ 19:05

        How do I find these config files?

  • Raven Maxwell Mar 31, 2021 @ 23:10

    Thank you for writing this one, I owe you a big time. I can’t believe such thing needed on Ubuntu. Just got over 1GB disk space on my Linode server. I use FreeBSD but Linode don’t have FreeBSD so I ended up with Ubuntu 18.04 LTS server.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum