User Tools

Site Tools


ubuntu:kernel:delete_old_kernel_images

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
ubuntu:kernel:delete_old_kernel_images [2019/12/15 22:15] – created peterubuntu:kernel:delete_old_kernel_images [2020/07/15 09:30] (current) – external edit 127.0.0.1
Line 40: Line 40:
 The **purge** removes packages and any configuration files are deleted too. 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:
 +
 +<code bash>
 +sudo apt --purge autoremove
 +</code>
 +
 +returns:
 +
 +<code bash>
 +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 ...
 +</code>
 +
 +----
 +
 +===== Delete kernel packages in remove/deinstall state =====
 +
 +<code bash>
 +sudo dpkg --list | grep linux-image
 +</code>
 +
 +returns:
 +
 +<code bash>
 +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
 +</code>
 +
 +  * **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 ====
 +
 +<code bash>
 +sudo x=$(dpkg --list | grep -i linux-image | grep ^rc| awk '{ print $2}')
 +sudo echo "$x"
 +sudo apt --purge remove $x
 +</code>
 +
 +----
 +
 +==== Recheck kernel package state ====
 +
 +<code bash>
 +sudo dpkg --list | grep linux-image
 +</code>
 +
 +returns:
 +
 +<code bash>
 +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
 +</code>
 +
 +Note that all **rc** states have gone.
 +
 +----
 +
 +===== Deleting all old kernels easily =====
 +
 +Get a list of kernels.
 +
 +<code bash>
 +# 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"
 +</code>
 +
 +To delete it simply run it as follows:
 +
 +<code bash>
 +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")
 +</code>
 +
 +----
 +
 +===== Easy way to delete all old kernels =====
 +
 +<code bash>
 +sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-]+//')")
 +</code>
 +
 +----
 +
 +===== Simple way to delete all old kernels =====
 +
 +<code bash>
 +sudo purge-old-kernels 
 +</code>
  
ubuntu/kernel/delete_old_kernel_images.1576448111.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki