What is the proper method to remove old kernels from a Red Hat Enterprise Linux system?
Environment
- Red Hat Enterprise Linux 3
- Red Hat Enterprise Linux 4
- Red Hat Enterprise Linux 5
- Red Hat Enterprise Linux 6
- Red Hat Enterprise Linux 7
- Red Hat Enterprise Linux 8
- Red Hat Enterprise Linux 9
- Red Hat Enterprise Linux 10
Issue
- How can I remove old kernel packages from a Red Hat Enterprise Linux system?
- Two kernel packages are appearing in my system, how to remove the older kernel package?
- My /boot directory is getting full, how do I remove old kernels?
# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/VolGroup00-LogVol00 14804452 7006156 7046272 50% / /dev/sda1 101086 101086 0 100% /boot none 127332 0 127332 0% /dev/shm
Resolution
Red Hat Enterprise Linux 8, 9, and 10
- The preferred method is to remove a specific older kernel. This differs from RHEL 5/6/7 in that kernel-core must be targeted:
# yum remove kernel-core-<version>
NOTE: the yum command cannot remove the running kernel. If you need to remove the running kernel you must reboot to a different kernel first.
- The YUM version 4 (based on the upstream
DNFproject) method for removing kernels and keeping only the latest version and running kernel:
# yum remove --oldinstallonly
Red Hat Enterprise Linux 5, 6, and 7
- The preferred method is to remove a specific older kernel:
# yum remove kernel-<version>
NOTE: the yum command cannot remove the running kernel. If you need to remove the running kernel you must reboot to a different kernel first.
- The
package-cleanupcommand can be used as shown below:
# package-cleanup --oldkernels
- From the
package-cleanupman 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
--countflag as shown. In the example below, the intent is to keep 3 kernels installed on the system and remove any others.
# package-cleanup --oldkernels --count=3
Red Hat Enterprise Linux 3 and 4
- The
rpmcommand can be used to remove a specific older kernel:
# rpm -e kernel-<version>
**Warning**: the `rpm` command can remove all kernel packages, we advise only removing specific kernels and not using a wildcard.
**Warning**: Removing all kernels will render the system unbootable.
**Note**: We suggest leaving two kernels minimum for troubleshooting.
Root Cause
- Kernels are packaged as RPMs. The underlying
rpmcommand can be used to query and interact with the installed kernels on a system. However, due to the critical nature of the kernel for a system, the following limitations are to be noted.
- Avoid removal of the running kernel
Doing so can cause unexpected failures to occur as kernel modules could be needed by the running kernel during runtime that are not already loaded. The attempt to load such a module that has been removed would result in inconsistent system behaviour and possible failure. It is always recommended to avoid removing the running kernel.
- During system updates, at minimum, two kernels should be retained/installed
The update process for a system to migrate between kernel versions requires a number of complex scripts to be executed successfully. In the event that there is a failure encountered within any, the update will proceed, but the resulting kernel installation may fail during a subsequent boot.
It is recommended to retain (at minimum) two kernel installations and associated dependencies to allow a previous working kernel to be selected during boot in the event that the default version fails.
- With these limitations in mind, using the underlying
rpmcommands to remove kernel versions is not recommended. The aboveyumbased methods include safeguards based around the above limitations.
Please see the following for further details regarding installation/removal of packages:
- Red Hat Enterprise Linux 5 - Chapter 14. YUM (Yellowdog Updater Modified)
- Red Hat Enterprise Linux 6 - Deployment Guide - Chapter 8. Yum
- Red Hat Enterprise Linux 7 - System Administrators Guide - 1.4. Installing Software
- Red Hat Enterprise Linux 8 - Configuring basic system settings - Chapter 2. Managing software packages
- Red Hat Enterprise Linux 9 - Managing software with the dnf tool - Chapter 8. Removing RHEL 9 content
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.