How to Remove Old Kernels on CentOS/RHEL 6/7

When you install new Kernel, the old Kernels will not remove because It can help you to boot server with old kernel if you do any mistake while installing the new Kernel. Also, the old kernel consumes some spaces which can be useful for something else. So In this article, we will provide you multiple methods to remove the old Kernel from your CentOS/RHEL server.

See Also:

Verify Kernel

In order to avoid any mistake first check the version of Kernel running on your system.

# uname -sr
Linux 3.10.0-957.5.1.el7.x86_64

List of Kernel

To list all installed kernels on your system, issue this command.

# kernel -q kernel
kernel-3.10.0-957.el7.x86_64
kernel-3.10.0-957.5.1.el7.x86_64
kernel-3.10.0-957.1.3.el7.x86_64

Remove Old Kernels RHEL

Method 1:
Run the commands below to remove a old Kernel along with its configuration files, then use update-grub2 command to update grub2 configuration.

# yum remove kernel-3.10.0-957.el7.x86_64
[[email protected] ~]# yum remove kernel-3.10.0-957.el7.x86_64
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-957.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================================================
Package Arch Version Repository Size
Removing:
kernel x86_64 3.10.0-957.el7 @anaconda 63 M
Transaction Summary
Remove 1 Package
Installed size: 63 M
Is this ok [y/N]:

Method 2:
You can also remove the old kernel using package-cleanup command which is a part of yum-utils package.

# yum install yum-utils
# package-cleanup --oldkernels --count=2
[[email protected] ~]# package-cleanup --oldkernels --count=2
Loaded plugins: fastestmirror
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-957.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================================================
Package Arch Version Repository Size
Removing:
kernel x86_64 3.10.0-957.el7 @anaconda 63 M
Transaction Summary
Remove 1 Package
Installed size: 63 M
Is this ok [y/N]:

Note: After running the above command, it will remove all old/unused kernels and keep latest 2 Kernels.

Method 3:
By default CentOS/RHEL will keep last 5 kernels on your system. installonly_limit parameter is define in yum configuration file. So if you can change the default value of installonly_limit to 2 then it will keep latest 2 kernel on your system and remove the remaining Kernels.

# vim /etc/yum.conf
 [main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=2
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

Read More: More about Kernel

Enjoy it!

Leave a Reply

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.