A simple script to safely remove old kernel packages from Fedora Linux, freeing up disk space.
Every time Fedora pushes a kernel update, a new kernel package gets installed alongside the old ones. This is intentional - having multiple kernels ensures your system can boot even if something goes wrong with the latest version. But over months and years, these accumulate.
User might be running Fedora on a laptop with limited storage, or perhaps user've been putting off that system upgrade because something just felt off. user type df -h and realize those old kernels have quietly taken up several gigabytes. user know they're there, but removing them manually feels risky. What if user remove the wrong one and your system won't boot?
I use Bash. It's already on every Fedora system. Nothing to install. Nothing to configure. Just make it executable and run it. If user can run Fedora, user can run this script.
The script needed to handle a few core tasks:
1. Identify what can be safely removed
The script queries dnf for all installed kernel packages, then sorts them by version. It keeps the most recent ones and flags the rest for potential removal.
2. Handle the NVIDIA question
Old kernel modules (specifically kmod-nvidia) often linger after kernel updates. The script checks which kept kernels have matching NVIDIA modules and removes orphaned ones. But I didn't want to assume everyone wanted this - so it's optional, with a prompt by default.
3. Show what's about to happen
Before any changes, the script displays exactly what will be removed and calculates how much disk space will be freed. This transparency matters because trust is earned, not assumed.
4. Respect the user's choice
Whether user want to preview first with dry-run, automate with a yes flag, or adjust how many kernels to keep - the script accommodates different workflows.
Under the hood, the script performs these operations:
Checks for sudo privileges (required for dnf operations)
Queries installed kernels via dnf list installed kernel
Sorts kernels by version, keeping the specified number
Identifies kernels matching ^(kernel|kernel-core|kernel-devel|kernel-modules)
If NVIDIA cleanup is enabled, checks akmod-nvidia versions against kept kernels
Displays removal candidates and space to be freed
Proceeds with removal after confirmation (or skips with --yes)
The -k flag controls how many kernels to keep. Default is 2 - the current running kernel plus one fallback. This gives user a safety net without hoarding old versions.
It's the tool I use on my own Fedora systems. When I type clean-kernel and see those old kernels get removed, I get back gigabytes of storage and a cleaner system. That's it. That's the whole thing. This script may won't win any awards or push the boundaries of what's possible. It's just a small utility that solves a specific problem honestly.