====== Ubuntu - Kernel - Delete Old Kernel Images ====== ===== List all installed Linux kernel images ===== dpkg --list | egrep -i --color 'linux-image|linux-headers' returns: ii linux-headers-4.15.0-70 4.15.0-70.79 all Header files related to Linux kernel version 4.15.0 ii linux-headers-4.15.0-70-generic 4.15.0-70.79 amd64 Linux kernel headers for version 4.15.0 on 64 bit x86 SMP ii linux-headers-4.15.0-72 4.15.0-72.81 all Header files related to Linux kernel version 4.15.0 ii linux-headers-4.15.0-72-generic 4.15.0-72.81 amd64 Linux kernel headers for version 4.15.0 on 64 bit x86 SMP ii linux-headers-generic 4.15.0.72.74 amd64 Generic Linux kernel headers rc linux-image-4.15.0-29-generic 4.15.0-29.31 amd64 Signed kernel image generic rc linux-image-4.15.0-55-generic 4.15.0-55.60 amd64 Signed kernel image generic rc linux-image-4.15.0-58-generic 4.15.0-58.64 amd64 Signed kernel image generic rc linux-image-4.15.0-60-generic 4.15.0-60.67 amd64 Signed kernel image generic rc linux-image-4.15.0-62-generic 4.15.0-62.69 amd64 Signed kernel image generic rc linux-image-4.15.0-64-generic 4.15.0-64.73 amd64 Signed kernel image generic rc linux-image-4.15.0-65-generic 4.15.0-65.74 amd64 Signed kernel image generic rc linux-image-4.15.0-66-generic 4.15.0-66.75 amd64 Signed kernel image generic rc linux-image-4.15.0-69-generic 4.15.0-69.78 amd64 Signed kernel image generic ii linux-image-4.15.0-70-generic 4.15.0-70.79 amd64 Signed kernel image generic ii linux-image-4.15.0-72-generic 4.15.0-72.81 amd64 Signed kernel image generic ii linux-image-generic 4.15.0.72.74 amd64 Generic Linux kernel image ---- ===== Delete unwanted and unused kernel images ===== sudo apt --purge remove linux-image-3.12.0-27-generic The **purge** removes packages and any configuration files are deleted too. On newer system all obsolete kernels and headers should automatically be flagged as not being needed, and thus can be purged with the following single command: sudo apt --purge autoremove returns: Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: linux-headers-3.12.0-27* linux-headers-3.12.0-27-virtual* 0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded. After this operation, 64.7 MB disk space will be freed. Do you want to continue [Y/n]? y (Reading database ... 218493 files and directories currently installed.) Removing linux-headers-3.12.0-27-virtual ... Removing linux-headers-3.12.0-27 ... ---- ===== Delete kernel packages in remove/deinstall state ===== sudo dpkg --list | grep linux-image returns: rc linux-image-4.15.0-29-generic 4.15.0-29.31 amd64 Signed kernel image generic rc linux-image-4.15.0-55-generic 4.15.0-55.60 amd64 Signed kernel image generic rc linux-image-4.15.0-58-generic 4.15.0-58.64 amd64 Signed kernel image generic rc linux-image-4.15.0-60-generic 4.15.0-60.67 amd64 Signed kernel image generic rc linux-image-4.15.0-62-generic 4.15.0-62.69 amd64 Signed kernel image generic rc linux-image-4.15.0-64-generic 4.15.0-64.73 amd64 Signed kernel image generic rc linux-image-4.15.0-65-generic 4.15.0-65.74 amd64 Signed kernel image generic rc linux-image-4.15.0-66-generic 4.15.0-66.75 amd64 Signed kernel image generic rc linux-image-4.15.0-69-generic 4.15.0-69.78 amd64 Signed kernel image generic ii linux-image-4.15.0-70-generic 4.15.0-70.79 amd64 Signed kernel image generic ii linux-image-4.15.0-72-generic 4.15.0-72.81 amd64 Signed kernel image generic ii linux-image-generic 4.15.0.72.74 amd64 Generic Linux kernel image * **rc**: means package is in remove/deinstall state and only config file exists. * **ii**: means package is in install state and it is 100% installed on the system. ---- ==== Remove all linux-image packages in rc state ==== sudo x=$(dpkg --list | grep -i linux-image | grep ^rc| awk '{ print $2}') sudo echo "$x" sudo apt --purge remove $x ---- ==== Recheck kernel package state ==== sudo dpkg --list | grep linux-image returns: ii linux-image-4.15.0-70-generic 4.15.0-70.79 amd64 Signed kernel image generic ii linux-image-4.15.0-72-generic 4.15.0-72.81 amd64 Signed kernel image generic ii linux-image-generic 4.15.0.72.74 amd64 Generic Linux kernel image Note that all **rc** states have gone. ---- ===== Deleting all old kernels easily ===== Get a list of kernels. # Get kernel version. v="$(uname -r | awk -F '-virtual' '{ print $1}')" # Create ignore list to avoid deleting the running kernel. i="linux-headers-virtual|linux-image-virtual|linux-headers-${v}|linux-image-$(uname -r)" # Display the list. echo dpkg --list | egrep -i 'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i" To delete it simply run it as follows: v="$(uname -r | awk -F '-virtual' '{ print $1}')" i="linux-headers-virtual|linux-image-virtual|linux-headers-${v}|linux-image-$(uname -r)" apt --purge remove $(dpkg --list | egrep -i 'linux-image|linux-headers' | awk '/ii/{ print $2}' | egrep -v "$i") ---- ===== Easy way to delete all old kernels ===== sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-]+//')") ---- ===== Simple way to delete all old kernels ===== sudo purge-old-kernels