Skip to content

Instantly share code, notes, and snippets.

@nukhes
Created September 13, 2025 18:52
Show Gist options
  • Save nukhes/c18212b2b3ed6d63cf2c48debef4d11a to your computer and use it in GitHub Desktop.
Save nukhes/c18212b2b3ed6d63cf2c48debef4d11a to your computer and use it in GitHub Desktop.
Cleanup old kernels in Debian based systems (Ubuntu, Pop!_OS, Linux Mint).
#!/bin/bash
set -e
current=$(uname -r)
echo "Current kernel: $current"
kernels=$(dpkg --list | grep linux-image-[0-9] | awk '{print $2}')
to_remove=$(echo "$kernels" | grep -v "$current")
if [ -z "$to_remove" ]; then
echo "No old kernels found."
exit 0
fi
echo "Removing:"
echo "$to_remove"
sudo apt remove --purge -y $to_remove
sudo apt autoremove --purge -y
sudo update-grub
echo "Cleanup done. Only the current kernel is kept."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment