Cleaning old kernels: Alternatives to package-cleanup --oldkernels?

How do you go about getting rid of old kernels automatically in Almalinux? I’ve been using package-cleanup with –oldkernels and –count set, but that doesn’t work on Alma. The man-page for package-cleanup says “A DNF-based shim imitating the original YUM-based package-cleanup utility.”, where –oldkernels isn’t listed as an option anymore…

To remove all but the latest two of every “installonly”-type package:

dnf remove $(dnf repoquery --installonly --latest-limit=-2 -q)
1 Like

Checking installed packages:

$ sudo dnf repoquery --installonly
Failed to set locale, defaulting to C.UTF-8
Failed loading plugin "product-id": No module named 'subscription_manager.productid'
Failed loading plugin "subscription-manager": cannot import name 'injection'
Failed loading plugin "upload-profile": cannot import name 'packageprofilelib'
kernel-0:4.18.0-305.25.1.el8_4.x86_64
kernel-0:4.18.0-348.2.1.el8_5.x86_64
kernel-0:4.18.0-348.el8.x86_64
kernel-core-0:4.18.0-305.25.1.el8_4.x86_64
kernel-core-0:4.18.0-348.2.1.el8_5.x86_64
kernel-core-0:4.18.0-348.el8.x86_64
kernel-modules-0:4.18.0-305.25.1.el8_4.x86_64
kernel-modules-0:4.18.0-348.2.1.el8_5.x86_64

Running the solution:

$ sudo dnf remove $(dnf repoquery --installonly --latest-limit=-2 -q)
Failed to set locale, defaulting to C.UTF-8
Failed loading plugin "product-id": No module named 'subscription_manager.productid'
Failed loading plugin "subscription-manager": cannot import name 'injection'
Failed loading plugin "upload-profile": cannot import name 'packageprofilelib'
Failed to set locale, defaulting to C.UTF-8
Failed loading plugin "product-id": No module named 'subscription_manager.productid'
Failed loading plugin "subscription-manager": cannot import name 'injection'
Failed loading plugin "upload-profile": cannot import name 'packageprofilelib'
Dependencies resolved.
==========================================================================================================
 Package                   Architecture      Version                             Repository          Size
==========================================================================================================
Removing:
 kernel                    x86_64            4.18.0-305.25.1.el8_4               @baseos              0  
 kernel-core               x86_64            4.18.0-305.25.1.el8_4               @baseos             67 M
 kernel-modules            x86_64            4.18.0-305.25.1.el8_4               @baseos             22 M

Transaction Summary
==========================================================================================================
Remove  3 Packages

Freed space: 88 M
Is this ok [y/N]: y
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                  1/1 
  Erasing          : kernel-4.18.0-305.25.1.el8_4.x86_64                                              1/3 
  Running scriptlet: kernel-4.18.0-305.25.1.el8_4.x86_64                                              1/3 
  Erasing          : kernel-modules-4.18.0-305.25.1.el8_4.x86_64                                      2/3 
  Running scriptlet: kernel-modules-4.18.0-305.25.1.el8_4.x86_64                                      2/3 
  Running scriptlet: kernel-core-4.18.0-305.25.1.el8_4.x86_64                                         3/3 
  Erasing          : kernel-core-4.18.0-305.25.1.el8_4.x86_64                                         3/3 
  Running scriptlet: kernel-core-4.18.0-305.25.1.el8_4.x86_64                                         3/3 
  Verifying        : kernel-4.18.0-305.25.1.el8_4.x86_64                                              1/3 
  Verifying        : kernel-core-4.18.0-305.25.1.el8_4.x86_64                                         2/3 
  Verifying        : kernel-modules-4.18.0-305.25.1.el8_4.x86_64                                      3/3 

Removed:
  kernel-4.18.0-305.25.1.el8_4.x86_64                   kernel-core-4.18.0-305.25.1.el8_4.x86_64          
  kernel-modules-4.18.0-305.25.1.el8_4.x86_64          

Complete!

Worked!

Hm, be careful: repoquery does query available packages and these may not be installed. What if the latest 2 kernels are not installed? You could end up trying to remove all installed kernels?

another way: in /etc/yum.conf

installonly_limit=2
1 Like

I suppose changing the yum.conf is the easier solution. However, the solution proposed by @jlehtone contains the –installonly switch, so it would only delete installed kernels, right?

Normal package replaces earlier version on install.
Install_only packages, like kernel, do not replace; you can have multiple versions installed at same time.

The repoquery returns a list of package-names.
True, it does not check what is installed. Must refine that somehow.

Dnf remove xxx does uninstall xxx only if it is installed.

Is there still protection for currently running kernel?

no.

here is a description from yum.conf(5)

installonlypkgs
              list

              List of provide names of  packages  that  should  only  ever  be
              installed,  never upgraded. Kernels in particular fall into this
              category.  These packages are never removed  by  dnf  autoremove
              even    if    they   were   installed   as   dependencies   (see
              clean_requirements_on_remove for auto  removal  details).   This
              option  append  the  list  values to the default installonlypkgs
              list used by DNF. The number of kept package versions  is  regu-
              lated by installonly_limit.

the --installonly option restricts to installonlypkgs

Ops. In that case, I’ll use installonly_limit=2 in the yum config-file.

Adding ‘–installed’ seems to limit the search suitably:
dnf repoquery --installed --installonly --latest-limit=-2 -q

1 Like

It seems that kernel-core package is protected - see protect_running_kernel in yum.conf - but not kernel or kernel-modules packages.

cool.
In the meantime I found

dnf [options] remove --oldinstallonly

this looked promising at first sight, but it doesn’t honor installonly_limit from yum.conf.