Back to Resources
Level
Script
Maintenance
Over time, Debian-based systems accumulate multiple kernel versions, consuming disk space and cluttering the bootloader. Manually removing old kernels is time-consuming and prone to errors. This script automates kernel cleanup, ensuring system stability while reclaiming storage.
This script identifies the currently running kernel and compiles a list of all installed kernel versions. It then removes all but the two most recent kernels, ensuring that the system maintains stability while reclaiming disk space. Once old kernels are removed, the script performs a cleanup operation using apt-get autoremove to delete any residual dependencies or unused packages. Finally, it provides a summary of how many kernels were removed, ensuring transparency and ease of use.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Level Library
# https://level.io/library/script-linux-debian-kernel-cleanup
# Get the current kernel version
current_kernel=$(uname -r)
# List all installed kernels, sorted in version order, and exclude the current kernel
installed_kernels=$(ls /lib/modules | grep -v "$current_kernel" | sort -V)
# Keep the two most recent kernels as backups
kernels_to_keep=$(echo "$installed_kernels" | tail -n 2)
# Create a list of kernels to remove, excluding the kernels to keep
kernels_to_remove=$(comm -23 <(echo "$installed_kernels") <(echo "$kernels_to_keep"))
if [ -n "$kernels_to_remove" ]; then
# Remove each kernel in the list
for kernel in $kernels_to_remove
do
echo "Removing $kernel"
sudo apt-get remove -y "linux-image-$kernel"
sudo apt-get purge -y "linux-image-$kernel"
sudo rm -rf "/lib/modules/$kernel"
done
# Finally, clean up any remaining unused packages
sudo apt-get autoremove -y
echo "Kernel cleanup complete!"
# Count the number of kernels removed
num_kernels_to_remove=$(echo "$kernels_to_remove" | wc -l)
else
echo "No kernels to remove."
num_kernels_to_remove=0
fi
echo "Number of kernels removed: $num_kernels_to_remove"
Linux - Debian Kernel Cleanup
This Bash script removes old Linux kernel versions while keeping the two most recent kernels as backups, ensuring the system stays clean and efficient by freeing up disk space and removing unused packages.
Bash
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.