Created
          September 13, 2025 18:52 
        
      - 
      
 - 
        
Save nukhes/c18212b2b3ed6d63cf2c48debef4d11a to your computer and use it in GitHub Desktop.  
    Cleanup old kernels in Debian based systems (Ubuntu, Pop!_OS, Linux Mint).
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/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