How To Remove Kernel In Ubuntu : Delete Old Or Unused Kernal

How To Remove Kernel In Ubuntu

This post will help you to remove any unused or old Linux kernel from your Ubuntu Linux operating system. It is also good move to remove old or unused Kernels from Linux as it occupies more space in /boot partition on Ubuntu.

How To Remove Kernel In Ubuntu | Delete Old Or Unused Kernal

DISCLAIMER: The following process of Kernels deletion from your systems might affect your Linux. Please be careful before your remove old or unused kernel.

Some of the things that you should do before removing your old or unused kernels are:

  • Backup your files or any important documents.
  • Note down the version of your Linux Kernel.

Linux kernels and their associated files are stored under the /boot directory and the Kernel modules are stored in /lib/modules directory of your system. The Linux kernel files names starting with “vmlinuz-” or “vmlinuz.” and end with the version number.

Run the following  commands to check all installed Linux Kernels in Debian, Ubuntu and its derivatives:

dpkg --list | grep linux-image

You will see the output with the list of installed Linux kernel in your Linux operating systems.

rc  linux-image-5.15.0-25-generic              5.15.0-25.25                            amd64        Signed kernel image generic
ii  linux-image-5.15.0-41-generic              5.15.0-41.44                            amd64        Signed kernel image generic

In above output, “rc” stands for “removed, but the configuration files are still present and  “ii” stands for “installed, and successfully installed.

Run the following command to remove old and unused Kernels automatically. sudo apt-get autoremove --purge command scans Linux system for packages that were installed as dependencies but are no longer needed. It also removes those packages and their associated configuration files. It helps to free up disk space and cleaning up the system by removing unnecessary packages.

sudo apt-get autoremove --purge

How to Purge Old Unused Kernels in Debian-based Systems

Run the following command to remove old unused Linux Kernels and keep only the currently running Linux Kernel.

$ echo $(dpkg --list | grep linux-image | awk '{ print $2 }' | sort -V | sed -n '/'`uname -r`'/q;p') $(dpkg --list | grep linux-headers | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p') | xargs sudo apt-get -y purge

Or,

Run the following command to remove old or unused Linux Kernel and keep only the currently running Linux Kernel.

$ sudo apt-get purge $(for tag in "linux-image" "linux-headers"; do dpkg-query -W -f'${Package}\n' "$tag-[0-9]*.[0-9]*.[0-9]*" | sort -V | awk 'index($0,c){exit} //' c=$(uname -r | cut -d- -f1,2); done)

How to Delete Old Unused Kernels in RHEL, Fedora, CentOS, Rocky Linux and AlmaLinux

Run the following  commands step by step to remove old unused Kernels in RHEL, Fedora, CentOS, Rocky Linux and AlmaLinux

1. Update your system:

sudo dnf update

2. Check the current kernel version:

This command will display the kernel name and version.

uname -sr

3. List all installed kernels:

sudo rpm -q kernel

4. Remove old kernels:

The following dnf command will keep the latest two kernels  and remove all the others:

sudo dnf remove --oldinstallonly --setopt installonly_limit=2 kernel

Leave a Comment