Clean-up old kernels in Fedora

Lars J├Ânsson 2022-09-22

Information about how to free up space on the boot partition in Fedora.

Clean-Up Old Kernels

After you boot into the latest kernel and test the system you can remove previous kernels. Old kernels remain even after dnf autoremove to avoid unintentional removals.

One of the easier ways to remove old kernels is with a script that retains the latest kernel. The script below works whenever Fedora updates a kernel, and does not depend upon a system upgrade.

#!/usr/bin/env bash

old_kernels=($(dnf repoquery --installonly --latest-limit=-1 -q))
if [ "${#old_kernels[@]}" -eq 0 ]; then
    echo "No old kernels found"
    exit 0
fi

if ! dnf remove "${old_kernels[@]}"; then
    echo "Failed to remove old kernels"
    exit 1
fi

echo "Removed old kernels"
exit 0

Info in the Fedora documentation

Change the old kernels limit

To prevent that the boot partition runs out of space, the limit for number of old kernels to save can be changed. The variable installonly_limit in /etc/dnf/dnf.conf specifies the number of kernels to retain on updates.

With the following setting, the system will remove all but the two latest kernels automatically on updates.

[main]
gpgcheck=True
installonly_limit=2
clean_requirements_on_remove=True
...