keepcalculator

06 Numbers and Powers

Delete Old Kernels in CentOS, RHEL & Fedora

Card No. 06·48110 · Updated · Standard formula, shown below

Yes, you should delete old kernels in CentOS, RHEL, and Fedora to free up disk space and avoid bootloader clutter, but keep at least one previous kernel as a fallback. The exact commands differ by distribution and version, but the core idea is the same: remove all but the newest (and one spare) kernel packages, then let the bootloader regenerate its configuration.

Why Old Kernels Accumulate

Every time you update the kernel via dnf or yum, the package manager installs the new version alongside the old ones. This is deliberate—if a new kernel fails to boot or has a regression, you can select the previous one from the GRUB menu. Over months or years, however, these kernels pile up. Each kernel package includes the kernel image, modules, and headers, often consuming 200–400 MB. On a system with a small /boot partition (common in older RHEL setups), this can fill up and cause updates to fail.

How to Check What’s Installed

Before deleting anything, see which kernels are present. On RHEL/CentOS 7 and older, use yum; on RHEL 8/9, CentOS Stream, and Fedora, use dnf. The command is the same for both:

rpm -q kernel

This lists all installed kernel packages, e.g., kernel-5.14.0-362.8.1.el9_3.x86_64. You can also check the contents of /boot with ls /boot to see the vmlinuz and initramfs files. The currently running kernel is shown by uname -r. Always keep that one, plus at least one older one.

Removing Kernels on RHEL/CentOS 7 and Older (yum)

On CentOS 7 and RHEL 7, yum has a convenient plugin called yum-utils that provides package-cleanup. Install it if needed:

sudo yum install yum-utils

Then remove old kernels while keeping the two newest:

sudo package-cleanup --oldkernels --count=2

This removes all kernels except the two most recent. If you prefer to do it manually, identify the kernel packages you want to remove and run:

sudo yum remove kernel-3.10.0-1160.99.1.el7.x86_64

Replace the package name with the one you want to delete. You can list multiple packages in one command.

Removing Kernels on RHEL 8/9, CentOS Stream, and Fedora (dnf)

On modern systems, dnf has a built-in option to remove old kernels automatically. The command:

sudo dnf remove --oldinstallonly

This removes all kernels except the currently running one and the newest one (if different). It respects the installonly_limit setting in /etc/dnf/dnf.conf, which defaults to 3. That means dnf keeps up to 3 kernel packages by default. To change that limit, edit the file and set installonly_limit=2 (or any number you want). After changing it, run sudo dnf remove --oldinstallonly again to prune extras.

If you want to remove a specific kernel manually, use:

sudo dnf remove kernel-6.6.8-200.fc39.x86_64

Automatic Cleanup with dnf-automatic or cron

You can automate kernel cleanup so you never have to think about it. On Fedora and RHEL 8+, the dnf-automatic package can be configured to apply updates and also remove old kernels. However, the simplest approach is to set installonly_limit=2 in dnf.conf. Then every time you run sudo dnf update, dnf will automatically remove the oldest kernel when a new one is installed, keeping only two. This is the most hands-off method and works for both Fedora and RHEL 8/9.

Common Pitfalls and Safety Tips

  • Never delete the running kernel. The system will still boot, but you won’t be able to load modules or recover easily. Always check uname -r first.
  • Keep at least one spare kernel. If the newest kernel has a problem (e.g., a driver issue), you need a fallback. Keeping two is a good balance.
  • GRUB will update automatically. When you remove a kernel package, the post-uninstall script runs grub2-mkconfig (or update-grub on some systems), so you don’t need to run it manually. However, if you manually delete files from /boot without using the package manager, GRUB will not update and you may end up with a broken boot entry.
  • Don’t use wildcards blindly. Commands like sudo rm /boot/vmlinuz-* are dangerous. Always use the package manager.
  • If /boot is full, update may fail. If you get an error saying “No space left on device” during a kernel update, boot into an older kernel (if possible) and run the cleanup commands above.

Frequently Asked Questions

Is it safe to delete old kernels?

Yes, as long as you keep the current kernel and at least one previous one. The package manager handles all the necessary cleanup, including removing initramfs files and updating the bootloader. The risk is minimal if you use the proper commands.

How much space will I free up?

Each kernel package typically consumes 200–400 MB, including modules and headers. If you have 5 old kernels, you could free 1–2 GB. The exact amount depends on the kernel version and architecture (x86_64 vs. ARM).

Can I delete kernels manually by removing files from /boot?

Technically yes, but it’s not recommended. The package manager tracks the kernel packages, and manual deletion can leave orphaned entries in the RPM database and GRUB. Always uninstall via dnf or yum.

What is installonly_limit and how do I change it?

installonly_limit is a setting in /etc/dnf/dnf.conf that controls how many versions of installonly packages (like kernels) are kept. The default is 3. To change it, edit the file and add or modify the line installonly_limit=2, then save. The next time you update, older kernels will be removed automatically.

Why does GRUB still show old kernels after I removed them?

If GRUB still lists old kernels, it’s likely because the bootloader configuration wasn’t regenerated. This can happen if you manually deleted files. Run sudo grub2-mkconfig -o /boot/grub2/grub.cfg (on UEFI systems, the path is /boot/efi/EFI/redhat/grub.cfg or similar). This will rebuild the menu based on the installed kernel packages.

Can I keep only the current kernel and delete all others?

Yes, but it’s not recommended. If the current kernel has a problem, you’ll have no fallback. If you’re confident, you can set installonly_limit=1, but most administrators keep at least two.

Alternatives and Best Practices

Some administrators prefer to keep only the current and previous kernel, while others keep three. The installonly_limit setting gives you control. For servers, a common practice is to set it to 2 or 3 to balance stability and disk usage. For desktop systems, 2 is usually enough. Also, consider cleaning up orphaned packages with sudo dnf autoremove after removing kernels—this removes dependencies that are no longer needed, freeing additional space.

If you’re using a custom kernel (e.g., from ELRepo), the same commands work, but the package names will differ. Always check the package list with rpm -q kernel to ensure you’re removing the right ones.

Finally, remember that kernel cleanup is a routine maintenance task. By setting installonly_limit and using the built-in dnf remove --oldinstallonly, you can keep your system clean without manual intervention.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →