
Safe Old Kernel Cleanup With DNF: Commands and Checks
Use sudo dnf remove --oldinstallonly when you need DNF to remove old kernels. Run uname -r first, inspect the installed packages, and preview the transaction before accepting it. Keep the running kernel and one known-good fallback. Old kernels belong in the package manager, not in rm. Deleting files from /boot or /lib/modules leaves the package database lying about what is installed.
Last updated: 2026-11-05
What is the safe DNF command to remove old kernels?
Run these steps in order. The preview matters because it shows the complete transaction before DNF changes the machine.
- Print the running kernel.
uname -r
The -r option prints the active kernel release. Write that value down. You should still see it in the installed package list after cleanup.
- List the installed kernel packages.
rpm -qa | sort -V | grep '^kernel'
This shows Rocky Linux and AlmaLinux kernel packages in version order. Expect related packages such as kernel-core, kernel-modules, and kernel-modules-extra.
- Preview automatic cleanup.
sudo dnf remove --oldinstallonly --assumeno
The --assumeno option answers no at the confirmation prompt. Check the output for the running kernel, unexpected dependencies, or packages unrelated to the kernel.
- Remove obsolete install-only packages.
sudo dnf remove --oldinstallonly
DNF removes old install-only packages while keeping the latest versions and the running kernel. Read the transaction once more before you confirm it.
- Verify what remains.
rpm -qa | sort -V | grep '^kernel'
sudo grubby --info=ALL
df -h /boot /
These commands check the installed packages, boot entries, and available space. Do this before rebooting, while you still have a running system to inspect.
The DNF command reference documents the removal options. However, the transaction shown on your machine is the evidence that matters.
Why sudo dnf remove --oldinstallonly is the right default
The --oldinstallonly option targets obsolete install-only packages. Kernels belong to that class because several versions can remain installed together.
The command keeps the latest versions and the version of the running kernel. That makes it a better default than building a wildcard around whatever version string looks old.
Use sudo because removing installed RPM packages changes system files and the RPM database. Running the command without sufficient privileges should fail rather than half-work. A package transaction is one place where failure is preferable to improvisation.
I use explicit package removal only when I need to diagnose one release. For routine cleanup, sudo dnf remove --oldinstallonly lets DNF apply its own install-only rules.
Check the running kernel before removing anything
Run this first:
uname -r
That output is the kernel currently running the machine. Now compare it with the installed package list:
rpm -qa | sort -V | grep '^kernel'
A Linux kernel is usually split across several packages. On Rocky Linux and AlmaLinux, one release may have matching entries for:
kernelkernel-corekernel-moduleskernel-modules-corekernel-modules-extrakernel-devel
Do not treat each line as a separate kernel. Match packages by their version and release, then compare that group with uname -r.
Next, check the default boot entry:
sudo grubby --default-kernel
uname -r tells you what is running now. grubby tells you what the bootloader plans to start next. If those point to different releases, stop and find out why before removing either one.
Keep a known-good fallback kernel. A newer kernel can boot cleanly several times and still expose a driver regression later. The fallback earns its disk space the night a storage or network driver stops loading.
Rocky Linux and AlmaLinux kernel cleanup
Rocky Linux and AlmaLinux use the same DNF workflow for this job. Start with the running release and the installed RPM packages:
uname -r
rpm -qa | sort -V | grep '^kernel'
Then preview the automatic cleanup:
sudo dnf remove --oldinstallonly --assumeno
If the transaction keeps the active release and a fallback, run it without --assumeno:
sudo dnf remove --oldinstallonly
Rocky Linux splits kernel files across packages such as kernel, kernel-core, and kernel-modules. AlmaLinux follows the same general packaging model. DNF knows how those pieces relate. A file deletion does not.
Afterward, verify the boot entries:
sudo grubby --info=ALL
Also inspect the package list again. A successful Rocky or AlmaLinux kernel cleanup leaves the package database, /boot, and the bootloader entries in agreement.
These commands belong to the Red Hat family. Ubuntu and Debian use apt and dpkg, so do not carry DNF instructions over by habit. If that is the system in front of you, read why APT may leave old kernels behind before removing packages.
Match kernel packages by name, version, and NEVRA
Use an exact package identifier when automatic cleanup is too broad. NEVRA means name, epoch, version, release, and architecture. Together, those fields identify one RPM package without guesswork.
Print those fields for installed kernel packages:
rpm -qa --qf '%{NAME}-%{EPOCHNUM}:%{VERSION}-%{RELEASE}.%{ARCH}\n' 'kernel*' | sort -V
Then preview removal using the exact package identifier shown by RPM:
sudo dnf remove --assumeno '<exact-kernel-package-nevra>'
If the transaction is correct, repeat it without --assumeno:
sudo dnf remove '<exact-kernel-package-nevra>'
Quote globs when you use them. Otherwise, the shell may expand the pattern against files in the current directory before DNF sees it.
sudo dnf remove --assumeno 'kernel-core-<version-release>.*'
Still, exact NEVRA removal is safer for diagnosis. A broad kernel* wildcard can catch development packages, headers, tools, and every installed runtime release. That is a poor way to save a little space.
Use only package names that appeared in your RPM output. Some installations do not have every split package, and copying a fixed removal list creates more confusion than it solves.
Inspect the transaction before confirming removal

The proposed transaction is the most useful output DNF gives you. Read the package names, dependency changes, and total action before pressing y.
A clean old-kernel transaction should contain obsolete kernel packages and their matching components. Stop if you see the running release, a large unrelated package group, or software you still use.
For an automatic cleanup preview, run:
sudo dnf remove --oldinstallonly --assumeno
For one exact release, use its full package identifier:
sudo dnf remove --assumeno '<exact-kernel-package-nevra>'
Do not add -y while diagnosing. The assume-yes option is useful in controlled automation, but it removes the pause where a human might notice DNF taking half the system with it.
If a transaction fails, inspect DNF history:
sudo dnf history list
sudo dnf history info <transaction-id>
History tells you what DNF attempted, which packages changed, and whether the transaction completed. Treat rollback as a recovery tool, not an undo button you can trust blindly. Repository state and package availability may have changed since the original transaction.
Autoremove is not the primary kernel cleanup tool
Preview autoremove before accepting it:
sudo dnf autoremove --assumeno
DNF autoremove targets packages marked as unused dependencies. Install-only packages, particularly kernels, are never removed by dnf autoremove even when they were originally installed as dependencies.
That is why dnf autoremove does not replace dnf remove --oldinstallonly. They solve different package-state problems.
If the preview looks sensible, you can run:
sudo dnf autoremove
However, read every package in the proposed transaction. DNF is following dependency metadata, not your memory of why the package was installed.
I do not accept a large autoremove transaction because the word “unused” sounds harmless. If the list surprises you, inspect the package first:
dnf repoquery --installed --info '<package-name>'
That output helps establish the installed version and package metadata before you remove it.
Configure the install-only package limit
Set installonly_limit if old kernels keep accumulating. This is the durable fix because it controls how many install-only versions DNF retains during future transactions.
DNF reads its global configuration from /etc/dnf/dnf.conf. Check the file before changing it:
sudo grep -nE '^\s*(installonly_limit|installonlypkgs)\s*=' /etc/dnf/dnf.conf
If the DNF config-manager command is available, inspect the effective setting:
dnf config-manager --dump | grep -E '^(installonly_limit|installonlypkgs)'
Set the limit under the [main] section:
[main]
installonly_limit=<count>
Choose a count that leaves the active kernel and a known-good fallback. Do not reduce it merely to make /boot look tidy.
Also check for command-line overrides, automation, and configuration management that rewrites dnf.conf. Editing the right file once does nothing if another tool replaces it during the next update.
After changing the setting, run a preview:
sudo dnf remove --oldinstallonly --assumeno
That tells you how the effective policy behaves against the packages actually installed.
Protected packages and failed removals
A protected-package message is DNF stopping a dangerous transaction. Read the named package and the reason before changing any protection rule.
First, capture the complete output:
sudo dnf remove --oldinstallonly --assumeno 2>&1 | tee /tmp/dnf-old-kernels.txt
Then inspect the relevant configuration:
sudo grep -RniE 'protected_packages|installonly' \
/etc/dnf/dnf.conf \
/etc/dnf/protected.d \
/etc/yum/protected.d 2>/dev/null
Protection overrides can come from the main configuration or drop-in files. Do not disable protection globally to force one transaction through. That turns a useful refusal into a much larger package problem.
Check the DNF logs next:
sudo tail -n 100 /var/log/dnf.log
sudo tail -n 100 /var/log/dnf.rpm.log
The solver message and RPM log usually tell you whether the block came from package protection, dependency resolution, or an RPM script failure.
Use strace only when DNF appears unable to read a file, open a database, or execute a helper. It is not a package solver.
sudo strace -f -o /tmp/dnf-remove.strace \
dnf remove --oldinstallonly --assumeno
Then search the trace for failed system calls:
grep -E 'ENOENT|EACCES|EROFS|ENOSPC' /tmp/dnf-remove.strace
That tells you whether DNF hit a missing file, permission failure, read-only filesystem, or full filesystem. It does not tell you which kernel policy you intended.
Why dnf clean old kernels does not remove installed kernels
dnf clean old kernels is not a kernel-removal strategy. DNF cache cleaning and installed-package removal are separate operations.
For example, this command removes cached RPM packages:
sudo dnf clean packages
It does not uninstall kernels. Likewise, broader cache cleanup can remove downloaded metadata and cached package data, but the RPM database still records every installed kernel.
Use cache cleanup when /var or the root filesystem is short on space. Use this for installed old kernels:
sudo dnf remove --oldinstallonly
The distinction matters because clearing the cache may appear to recover space while leaving /boot unchanged. Then the next kernel update fails in exactly the same place. Linux quirks are generous that way.
Rebuild boot entries and verify the next reboot
Do not rebuild everything after a successful package transaction. First, prove something is stale.
Check the installed packages, boot entries, and files:
rpm -qa | sort -V | grep '^kernel'
sudo grubby --info=ALL
ls -lh /boot/vmlinuz-* /boot/initramfs-*
df -h /boot /
Each boot entry should point to an installed kernel and a matching initramfs. The active release from uname -r should remain installed, along with the fallback you chose to keep.
Rocky Linux and AlmaLinux normally update boot files through the kernel package scripts. You should not run grub2-install because a kernel was removed. That command changes the bootloader installation and does not belong in routine cleanup.
If an installed kernel has a missing or damaged initramfs, rebuild that release only:
sudo dracut --force --kver '<installed-kernel-release>'
Use a release that still appears in the RPM output. Rebuilding an initramfs for a package you already removed only creates another orphan.
Before rebooting, check the default entry again:
sudo grubby --default-kernel
If you need to recover from a bad next boot, use the retained kernel. The kernel rollback procedure covers selecting the safe GRUB entry without turning one regression into a reinstall.
Diagnose recurring kernel accumulation instead of deleting files
Repeated buildup means the policy or transaction path is broken. Deleting files hides the evidence and leaves RPM convinced those files still exist.
Start with the effective install-only configuration:
dnf config-manager --dump | grep -E '^(installonly_limit|installonlypkgs)'
Then compare the installed package state:
rpm -qa | sort -V | grep '^kernel'
Check recent DNF transactions:
sudo dnf history list
sudo grep -iE 'kernel|error|failed' /var/log/dnf.log | tail
sudo grep -iE 'kernel|error|failed' /var/log/dnf.rpm.log | tail
A failed kernel transaction can leave matching packages in an uneven state. For example, kernel-core may install while an initramfs script fails because /boot is full.
Also inspect the previous boot if kernel installation or boot-entry generation failed:
journalctl -b -1 -p err
That shows errors from the prior boot. Pair the timestamps with the DNF transaction instead of grepping random logs with no time anchor.
Finally, inspect the boot entries:
sudo grubby --info=ALL
If the packages are pruned but entries keep returning, the problem is boot-entry generation. If old packages remain despite the configured limit, inspect failed transactions, configuration overrides, and repository metadata. That is where the recurring cause lives.
For future updates, plan the fallback and reboot path before automation runs. This guide to safe kernel updates by distribution covers that maintenance cycle.
FAQ
What if /boot is full before DNF can remove an old kernel?
Run an exact removal preview against the oldest installed release:
sudo dnf remove --assumeno '<exact-old-kernel-nevra>'
If the transaction only removes that release and its matching packages, proceed through DNF. Do not delete an initramfs or vmlinuz file to make room. RPM will still record the package as installed, and the next transaction inherits the damage.
If the root filesystem is full instead, sudo dnf clean packages can clear cached RPM files. Check df -h /boot / first so you know which filesystem is actually out of space.
Can I remove an old kernel-devel package and keep the runtime kernel?
Yes, provided nothing still needs that development package. kernel-devel supplies files used to build external modules; it is not the runtime kernel image.
Preview the exact package removal:
sudo dnf remove --assumeno '<exact-kernel-devel-nevra>'
Check for Dynamic Kernel Module Support or third-party module builds tied to that release before confirming. Removing the development package does not remove a matching runtime kernel unless the proposed transaction says it will.
What should I do if the machine panics after cleanup?
Boot the retained fallback kernel from the GRUB menu. Once the system is up, confirm the running release with uname -r and inspect the previous boot:
journalctl -b -1 -p err
If the panic started with the newer kernel, keep the fallback as the default until you identify the driver, module, or initramfs failure. Do not remove the only working kernel while debugging the broken one.
Does ModuleJail remove old kernels?
No. ModuleJail writes a modprobe.d blacklist for kernel modules that are not currently in use, minus its baseline and any administrator whitelist. It does not uninstall kernel RPM packages or clean /boot.
Old kernel cleanup belongs to DNF. Module policy is a separate job, covered in the guide to kernel module blacklisting.
