DEV Community

Rakesh Jain
Rakesh Jain

Posted on

The Best and Safest way to clean up boot partition-Ubuntu 14.04/16.04/18.04 LTS

Alt Text

When you have apt working and boot partition has some space left :

Check the current kernel version
$ uname -r
3.19.0-64-generic

Remove the OLD kernels
List the old kernel (it wont include the current kernel)

$ sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v `uname -r`

You will get the list of images something like below:

linux-image-3.19.0-25-generic
linux-image-3.19.0-56-generic
linux-image-3.19.0-58-generic
linux-image-3.19.0-59-generic
linux-image-3.19.0-61-generic
linux-image-3.19.0-65-generic
linux-image-extra-3.19.0-25-generic
linux-image-extra-3.19.0-56-generic
linux-image-extra-3.19.0-58-generic
linux-image-extra-3.19.0-59-generic
linux-image-extra-3.19.0-61-generic

Now its time to remove old kernel one by one as

$sudo apt-get purge linux-image-3.19.0-25-generic
$ sudo apt-get purge linux-image-3.19.0-56-generic
$ sudo apt-get purge linux-image-3.19.0-58-generic
$ sudo apt-get purge linux-image-3.19.0-59-generic
$ sudo apt-get purge linux-image-3.19.0-61-generic
$ sudo apt-get purge linux-image-3.19.0-65-generic

When you’re done removing the older kernels, you can run this to remove ever packages you won’t need anymore:
$sudo apt-get autoremove

And finally you can run this to update grub kernel list:
$sudo update-grub

When boot partition has zero space left and apt not working:

Get the list of kernel images (it wont include the current kernel)
Get the list of kernel images and determine what you can do without.

This command will show installed kernels except the currently running one
$sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -vuname -r

You will get the list of images something like below:

linux-image-3.19.0-25-generic
linux-image-3.19.0-56-generic
linux-image-3.19.0-58-generic
linux-image-3.19.0-59-generic
linux-image-3.19.0-61-generic
linux-image-3.19.0-65-generic
linux-image-extra-3.19.0-25-generic
linux-image-extra-3.19.0-56-generic
linux-image-extra-3.19.0-58-generic
linux-image-extra-3.19.0-59-generic
linux-image-extra-3.19.0-61-generic

Prepare for old kernel delete
Craft a command to delete all files in /boot for kernels that don’t matter to you using brace expansion to keep you sane. Remember to exclude the current and two newest kernel images. From above Example, it’s
$sudo rm -rf /boot/*-3.19.0-{25,56,58,59,61,65}-*

Clean up apt for old partial installs
sudo apt-get -f install

Run Autoremove to clear old images
Finally, autoremove to clear out the old kernel image packages that have been orphaned by the manual boot clean.
$sudo apt-get autoremove

Update Grub
$sudo update-grub

Now you can update and install packages
sudo apt-get update
sudo apt-get install lshw -y

For an all in one try this:
sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v uname -r | while read -r line; do sudo apt-get -y purge $line;done;sudo apt-get autoremove; sudo update-grub; apt-get update; apt-get install lshw -y

Ref:https://help.ubuntu.com/community/RemoveOldKernels#Configure_Unattended_Upgrades_to_Remove_Unneeded_Kernels_Automatically

Hope you like the tutorial. Please like & share and let me know your feedback in the response section.
Happy Learning!

Top comments (0)