Red Hat Enterprise Linux 5, 6, and 7
=============================
The package-cleanup command can be used as shown below:
sudo package-cleanup --oldkernels
From the package-cleanup man page:
–oldkernels Remove old kernel and kernel-devel packages
–count=KERNELCOUNT
Number of kernel packages to keep on the system
(default 2)
The number of kernels left in place on a system can be selected using the –count flag as shown. In the example below, the intent is to keep 3 kernels installed on the system and remove any others.
sudo package-cleanup --oldkernels --count=3
Red Hat Enterprise Linux 8 and 9
===========================
The YUM version 4 (based on the upstream DNF project) method for removing kernels and keeping only the latest version and running kernel:
sudo yum remove --oldinstallonly
From the yum man page:
dnf [options] remove –oldinstallonly
Removes old installonly packages, keeping only latest versions and version of running
kernel.
In the instance where a user-defined number of kernels should be retained (similar to the –count functionality for package-cleanup), the following can be used. In the example below, the intent is to retain the latest 3 installonly packages:
sudo yum remove $(yum repoquery --installonly --latest-limit=-3 -q)
From the yum man page:
–latest-limit <number>
Limit the resulting set to <number> of latest packages for every package name and archi‐
tecture. If <number> is negative, skip <number> of latest packages. For a negative
<number> use the –latest-limit=<number> syntax.
To remove a single specific kernel, we want to target the kernel-core package.
# yum remove kernel-core-<VERSION>
NOTE: yum command cannot remove the running kernel. If you remove the specific kernel from the system, you need to reboot the system with another kernel to remove the specific kernel.