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

In this article, we will show how to remove old/unused kernel images on RHEL/CentOS/Fedora systems. However, before you remove an old kernel, it is important to keep your kernel up to date; install the latest version in order to leverage new kernel functions and to protect your system from vulnerabilities that have been discovered in older versions.

To install or upgrade to latest kernel version in RHEL/CentOS/Fedora systems, read this guide:

  1. How to Install or Upgrade to Latest Kernel Version in CentOS 7

Attention: On the contrary, is recommended to keep at least one or two old kernels to fall back to in case there is a problem with an update.

To display the current version of Linux (kernel) running on your system, run this command.

# uname -sr

Linux 3.10.0-327.10.1.el7.x86_64

List All Installed Kernels on System

You can list all kernel images installed on your system like this.

# rpm -q kernel

kernel-3.10.0-229.el7.x86_64
kernel-3.10.0-229.14.1.el7.x86_64
kernel-3.10.0-327.3.1.el7.x86_64
kernel-3.10.0-327.10.1.el7.x86_64

Removing Old/Unused Kernels on CentOS/RHEL

You need to install yum-utils, which is an assortment of utilities that integrate with yum to make it more powerful and easier to use, by extending its original features in several different ways.

# yum install yum-utils

One of these utilities is package-cleanup which you can use to delete old kernel as shown below, the count flag is used to specify the number of kernels you want to leave on the system.

# package-cleanup --oldkernels --count=2
Removing Old Kernels
Loaded plugins: fastestmirror, langpacks, product-id, versionlock
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-229.el7 will be erased
---> Package kernel.x86_64 0:3.10.0-229.14.1.el7 will be erased
---> Package kernel-devel.x86_64 0:3.10.0-229.1.2.el7 will be erased
---> Package kernel-devel.x86_64 0:3.10.0-229.14.1.el7 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================================
 Package                                       Arch                                    Version                                                Repository                                  Size
===============================================================================================================================================================================================
Removing:
 kernel                                        x86_64                                  3.10.0-229.el7                                         @anaconda                                  131 M
 kernel                                        x86_64                                  3.10.0-229.14.1.el7                                    @updates                                   131 M
 kernel-devel                                  x86_64                                  3.10.0-229.1.2.el7                                     @updates                                    32 M
 kernel-devel                                  x86_64                                  3.10.0-229.14.1.el7                                    @updates                                    32 M

Transaction Summary
===============================================================================================================================================================================================
Remove  4 Packages

Installed size: 326 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : kernel-devel.x86_64                            1/4 
  Erasing    : kernel.x86_64                                  2/4 
  Erasing    : kernel-devel.x86_64                            3/4 
  Erasing    : kernel.x86_64                                  4/4 
Loading mirror speeds from cached hostfile
 * base: centos.mirror.snu.edu.in
 * epel: repo.ugm.ac.id
 * extras: centos.mirror.snu.edu.in
 * rpmforge: kartolo.sby.datautama.net.id
 * updates: centos.mirror.snu.edu.in
  Verifying  : kernel-3.10.0-229.el7.x86_64                   1/4 
  Verifying  : kernel-devel-3.10.0-229.14.1.el7.x86_64        2/4 
  Verifying  : kernel-3.10.0-229.14.1.el7.x86_64              3/4 
  Verifying  : kernel-devel-3.10.0-229.1.2.el7.x86_64         4/4 

Removed:
  kernel.x86_64 0:3.10.0-229.el7           kernel.x86_64 0:3.10.0-229.14.1.el7           kernel-devel.x86_64 0:3.10.0-229.1.2.el7           kernel-devel.x86_64 0:3.10.0-229.14.1.el7          

Complete!

Important: After running the above command, it will remove all old/unused kernels and keep the current running and old latest kernel as backup.

Removing Old/Unused Kernels on Fedora

Fedora now uses dnf package manager, a new version of yum package manager, so you need to use this command below to remove old kernels on Fedora.

# dnf remove $(dnf repoquery --installonly --latest-limit 2 -q) 

Another alternative way to remove old kernels automatically is setting the kernel limit in yum.conf file as shown.

installonly_limit=2		#set kernel count

Save and close the file. The next time you run an update, only two kernels will be left on the system.

You may also like to read these following related articles on Linux kernel.

  1. How to Load and Unload Kernel Modules in Linux
  2. How to Upgrade Kernel to Latest Version in Ubuntu
  3. How to Change Kernel Runtime Parameters in a Persistent and Non-Persistent Way

In this article, we described how to remove old/unused kernel images on RHEL/CentOS/Fedora systems. You can share any thoughts via the feedback from below.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

13 Responses

  1. disp says:

    Configuration file for dnf: /etc/dnf/dnf.conf.

  2. noone says:

    Correct way to remove old kernels – there might be other ways but this works for sure:

    # dnf repoquery --installonly --latest-limit 99 -q  > /tmp/allkernels
    # 2 below can be any number that you want to keep - they will be the newest
    # dnf repoquery --installonly --latest-limit 2 -q  > /tmp/keepkernels
    # comm -1 -3 /tmp/keepkernels /tmp/allkernels > /tmp/removekernels
    # dnf  --noautoremove remove $(cat /tmp/removekernels)
    

    The dnf command you list is wrong still and whoever did the Fedora wiki page is wrong. The below shows that –latest-limit 1 goes from newest to oldest, not the other way around. In F26/F27 package-cleanup does not work and the command you show does not work – it removes the newer kernels.

    [[email protected] ~]# uname -r
    4.14.11-300.fc27.x86_64
    
    [[email protected] ~]# dnf repoquery --installonly --latest-limit 1 -q
    kernel-0:4.14.11-300.fc27.x86_64
    kernel-core-0:4.14.11-300.fc27.x86_64
    kernel-devel-0:4.14.11-300.fc27.x86_64
    kernel-modules-0:4.14.11-300.fc27.x86_64
    kernel-modules-extra-0:4.14.11-300.fc27.x86_64
    
    [[email protected] ~]# dnf repoquery --installonly --latest-limit 2 -q
    kernel-0:4.14.11-300.fc27.x86_64
    kernel-0:4.14.8-300.fc27.x86_64
    kernel-core-0:4.14.11-300.fc27.x86_64
    kernel-core-0:4.14.8-300.fc27.x86_64
    kernel-devel-0:4.14.11-300.fc27.x86_64
    kernel-devel-0:4.14.8-300.fc27.x86_64
    kernel-modules-0:4.14.11-300.fc27.x86_64
    kernel-modules-0:4.14.8-300.fc27.x86_64
    kernel-modules-extra-0:4.14.11-300.fc27.x86_64
    kernel-modules-extra-0:4.14.8-300.fc27.x86_64
    
    [[email protected] ~]# dnf repoquery --installonly --latest-limit 5 -q
    kernel-0:4.14.11-300.fc27.x86_64
    kernel-0:4.14.5-300.fc27.x86_64
    kernel-0:4.14.6-300.fc27.x86_64
    kernel-0:4.14.7-300.fc27.x86_64
    kernel-0:4.14.8-300.fc27.x86_64
    kernel-core-0:4.14.11-300.fc27.x86_64
    kernel-core-0:4.14.5-300.fc27.x86_64
    kernel-core-0:4.14.6-300.fc27.x86_64
    kernel-core-0:4.14.7-300.fc27.x86_64
    kernel-core-0:4.14.8-300.fc27.x86_64
    kernel-devel-0:4.14.11-300.fc27.x86_64
    kernel-devel-0:4.14.5-300.fc27.x86_64
    kernel-devel-0:4.14.6-300.fc27.x86_64
    kernel-devel-0:4.14.7-300.fc27.x86_64
    kernel-devel-0:4.14.8-300.fc27.x86_64
    kernel-modules-0:4.14.11-300.fc27.x86_64
    kernel-modules-0:4.14.5-300.fc27.x86_64
    kernel-modules-0:4.14.6-300.fc27.x86_64
    kernel-modules-0:4.14.7-300.fc27.x86_64
    kernel-modules-0:4.14.8-300.fc27.x86_64
    kernel-modules-extra-0:4.14.11-300.fc27.x86_64
    kernel-modules-extra-0:4.14.5-300.fc27.x86_64
    kernel-modules-extra-0:4.14.6-300.fc27.x86_64
    kernel-modules-extra-0:4.14.7-300.fc27.x86_64
    kernel-modules-extra-0:4.14.8-300.fc27.x86_64
    
    • noone says:

      Actually,

      # dnf repoquery --installonly --latest-limit=-1 -q
      

      Does list the older kernel. It looks like =[nospace]-X is required for this to work in F27 at least, the wiki has no = and a space. Positive numbers go from new to old and negative numbers start at the oldest.

      My method above is longer but would allow you to pick random kernels for the keep list,

      [[email protected] ~]# dnf repoquery --installonly --latest-limit=-1 -q
      kernel-0:4.14.8-300.fc27.x86_64
      kernel-core-0:4.14.8-300.fc27.x86_64
      kernel-devel-0:4.14.8-300.fc27.x86_64
      kernel-modules-0:4.14.8-300.fc27.x86_64
      kernel-modules-extra-0:4.14.8-300.fc27.x86_64
      
      
      
      [[email protected] ~]# dnf repoquery --installonly --latest-limit 2 -q
      kernel-0:4.14.11-300.fc27.x86_64
      kernel-0:4.14.8-300.fc27.x86_64
      kernel-core-0:4.14.11-300.fc27.x86_64
      kernel-core-0:4.14.8-300.fc27.x86_64
      kernel-devel-0:4.14.11-300.fc27.x86_64
      kernel-devel-0:4.14.8-300.fc27.x86_64
      kernel-modules-0:4.14.11-300.fc27.x86_64
      kernel-modules-0:4.14.8-300.fc27.x86_64
      kernel-modules-extra-0:4.14.11-300.fc27.x86_64
      kernel-modules-extra-0:4.14.8-300.fc27.x86_64
      

      usage: dnf repoquery...

  3. Jason says:

    In Fedora 26 this worked:

    $ sudo dnf remove $(dnf repoquery –installonly –latest-limit=-2 -q)

  4. Michael says:
    $ sudo dnf remove $(dnf repoquery --installonly --latest-limit 2 -q)
    

    Error:
    Problem: The operation would result in removing the following protected packages: kernel-core

    That is because

    $ dnf repoquery --installonly --latest-limit 2 -q
    

    Lists your current kernel

    $ sudo dnf remove $(rpm -qa | awk '/^kernel/' | grep -v $(uname -r))
    

    Seems like a safer alternative.

  5. N30xMind says:

    Hi Aaron,

    Under Remove Old/Unused Kernels On Fedora. In the below command -2 hyphen must be removed in order for the command to work.

    # dnf remove $(dnf repoquery --installonly --latest-limit -2 -q)
    

    It should be,

      
    # dnf remove $(dnf repoquery --installonly --latest-limit 2 -q)  
    

    Thank you

  6. R. Hell says:

    “Attention: On the contrary, is recommended to keep at least one or two old kernels to fall back to in case there is a problem with an update”

    I fully agree on that. And you know why? Well.. RedHat managed to get the following bug to exist since 2015 and still not being fixed fully.

    https://access.redhat.com/solutions/2172881

    So yes, you need to be sure that you keep something that works.

    On the other hand, SUSE automatically can maintain a definable number of kernels and purge old kernels automatically if you wish.

    Now, SUSE is better tested, automatically tested, is supported better, cheaper, better tooled. You simply are wondering why someone would want to use fedora, centos or rhel. The cool parts that SUSE can accomplish are major version updates without problems. RH 6 to 7? Good luck with that. And SUSE also can do a full upgrade of a service pack, patches and you can fully (!) roll back too. In minutes.

    • Aaron Kili says:

      @R. Hell

      Thanks for the useful insights on openSUSE, i suppose it’s high time i started using it to realize some of the benefits you have mentioned above. Once again, many thanks for writing back.

  7. Mauricio López says:

    Hey Aaron,

    What if you want to remove a newer kernel. I.e. after a kernel update the system crash so you need to use the old one. Can one use this method to remove the newest and unused kernel, not the old one?
    Thank you for your excellent articles!

    • Aaron Kili says:

      @Mauricio

      Not really, you can run this command to remove a kernel(preferably a new kernel):

      #yum remove kernel-4.12.10-1.el7.elrepo.x86_64

      OR

      #rpm – -erase kernel-4.12.10-1.el7.elrepo.x86_64

      Finally, thanks too for the useful feedback.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.