Skip to content

Instantly share code, notes, and snippets.

@ipbastola
Last active August 16, 2024 13:39
Show Gist options
  • Save ipbastola/2760cfc28be62a5ee10036851c654600 to your computer and use it in GitHub Desktop.
Save ipbastola/2760cfc28be62a5ee10036851c654600 to your computer and use it in GitHub Desktop.
Safest way to clean up boot partition - Ubuntu 14.04LTS-x64, Ubuntu 16.04LTS-x64

Safest way to clean up boot partition - Ubuntu 14.04LTS-x64, Ubuntu 16.04LTS-x64

Reference

Case I: if /boot is not 100% full and apt is working

1. Check the current kernel version

$ uname -r 

It will shows the list like below:

3.19.0-64-generic

2. Remove the OLD kernels

2.a. List the old 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

2.b. 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

Case II: Can't Use apt i.e. /boot is 100% full

NOTE: this is only if you can't use apt to clean up due to a 100% full /boot

1. Get the list of kernel images

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 -v `uname -r`

You will get the list of images somethign 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

2. Prepare 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}-*

3. Clean up what's making apt grumpy about a partial install.

sudo apt-get -f install

4. Autoremove

Finally, autoremove to clear out the old kernel image packages that have been orphaned by the manual boot clean.

sudo apt-get autoremove

5. Update Grub

sudo update-grub

6. Now you can update, install packages

sudo apt-get update
@Blackkumba
Copy link

thank you

@tango4j
Copy link

tango4j commented Dec 6, 2019

There was Jesus.
Jesus just messed around and didn't really fix important kernels.
After Jesus, ipbastola came down as a messiah to save us and free us from nasty evilest /boot 100% demon.

Thank you so much Thanks million times

@MarcSaric
Copy link

MarcSaric commented Jan 7, 2020

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

Should be

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

(there was a escaping issue in the original one-liner).

Otherwise great.

@ivanmara555
Copy link

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

Should be

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

(there was a escaping issue in the original one-liner).

Otherwise great.

see https://gist.github.com/ipbastola/2760cfc28be62a5ee10036851c654600#gistcomment-3047160

@mihaipora
Copy link

very helpful, thank you . Case II worked perfectly.

@jpcuve
Copy link

jpcuve commented Feb 10, 2020

Thank you

@matiasmasca
Copy link

matiasmasca commented Feb 11, 2020

Thanks! it works for me, I had a full disc error

@ray-moncada
Copy link

Thank you. I had to used both method several times and both worked.

@khuongln-1346
Copy link

khuongln-1346 commented Mar 9, 2020

Thanks very much. It works for me
https://gist.github.com/ipbastola/2760cfc28be62a5ee10036851c654600#gistcomment-3047160

my full automatic way including Ubuntu 18.04 :

apt purge $(dpkg --list linux-{headers,image,modules}-\* \
| awk '{ if ($1=="ii") print $2}' \
| egrep '[4-9]\.[0-9]+\.[0-9]+-[0-9]+' \
| egrep -v $(uname -r | cut -d"-" -f1,2))

apt autoremove
update-grub

@Tolgor
Copy link

Tolgor commented Mar 16, 2020

Life saver ! Thanks !

Ubuntu 16.04.6 LTS

@xenomorpheus
Copy link

xenomorpheus commented Apr 8, 2020

None of the usual methods worked, and when I manually deleted the files in /boot they returned.

This worked.

https://askubuntu.com/questions/563483/why-doesnt-apt-get-autoremove-remove-my-old-kernels/1225285#1225285

@ivanmara555
Copy link

ivanmara555 commented May 4, 2020

my full automatic way including Ubuntu 16.04, 18.04, 20.04 :

apt purge $(dpkg --list linux-{headers,image,modules}-\* \
| awk '{ if ($1=="ii") print $2}' \
| egrep '[0-9]+\.[0-9]+\.[0-9]+-[0-9]+' \
| egrep -v $(uname -r | cut -d"-" -f1,2))

apt autoremove
update-grub

@JacoJeco
Copy link

It works, thanks !

@aclarck
Copy link

aclarck commented Aug 13, 2020

Case 2 worked.
/boot is now clean
reboot ok

Thanks.

@gzjhones
Copy link

Muchas gracias, funciona perfecto, tendré que darle más espacio al boot la próxima vez

@ob-ivan
Copy link

ob-ivan commented Nov 17, 2020

I somehow feel more comfortable if instead of awk '{ if ($1=="ii") print $2}' i write awk '/^ii/ { print $2 }'.

@seth-rah
Copy link

seth-rah commented Nov 23, 2020

Case 1, 2b

The command can be automated if preferred.

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

@vmpr
Copy link

vmpr commented Dec 4, 2020

thanks, worked a treat!

@bllgg
Copy link

bllgg commented Feb 15, 2021

Thanks.

@djvdorp
Copy link

djvdorp commented Mar 2, 2021

Thanks a lot, much appreciated!

@Marty
Copy link

Marty commented Mar 18, 2021

Thank you and well done! It fixed my problem and I learned something :)

@NikoS34
Copy link

NikoS34 commented Apr 2, 2021

Tkx for the Case II (Can't Use apt i.e. /boot is 100% full), it rocks !

@danielfaust
Copy link

I used this gist to create a script which generates an "apt purge" command which can then be copy and pasted in a shell to execute it and free up some space. Hope it helps: https://gist.github.com/danielfaust/442a125ff5c5b17e60519a12f10f8113

@fahmimmaliki
Copy link

Thanks, it's help me a lot

@PeterTough2
Copy link

After removing one of the file returned after running this code, I rebooted my server and I can't access it any longer.
It is refusing connection, server is on AWS
AWS Instance status checks: Instance reachability check failed

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

Any idea how I can fix this.

@tomekowal
Copy link

Unfortunately, I had some "stubborn" linux images that were reinstalled every time I tried this solution.
Fortunately, I've found this script https://launchpad.net/linux-purge which helped :)

@JHLasbatre
Copy link

Perfect, thanks !

@pylapp
Copy link

pylapp commented Apr 6, 2022

The 2nd case saved me! Thank you a lot, you are awesome!

\(-ㅂ-)/ ♥ ♥ ♥

@Gmontalbano
Copy link

100% to 43% thanks

@khadishi
Copy link

khadishi commented Apr 3, 2023

so great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment