Instantly share code, notes, and snippets.

Embed
What would you like to do?
Safest way to clean up boot partition - Ubuntu 14.04LTS-x64

Safest way to clean up boot partition - Ubuntu 14.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
@michaelschem

This comment has been minimized.

michaelschem commented Sep 8, 2017

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

@GuillermoBlasco

This comment has been minimized.

GuillermoBlasco commented Sep 17, 2017

Working on 16.04 LTS as wll

@rico

This comment has been minimized.

rico commented Sep 20, 2017

Works, thanks.

@amitsaha

This comment has been minimized.

amitsaha commented Nov 1, 2017

THANK YOU

@jigewxy

This comment has been minimized.

jigewxy commented Nov 4, 2017

thanks, it worked perfectly.

@eggfriedrice

This comment has been minimized.

eggfriedrice commented Nov 17, 2017

Thanks for this, I ended up using dpkg directly to remove the old packages as apt kept refilling the partition every time I ran apt install -f!

@cptx86

This comment has been minimized.

cptx86 commented Dec 11, 2017

Thanks!

@sametserpil

This comment has been minimized.

sametserpil commented Dec 20, 2017

Thanks

@mwanamziki

This comment has been minimized.

mwanamziki commented Jan 8, 2018

Thanks, this has been of great help to me

@garrett-eclipse

This comment has been minimized.

garrett-eclipse commented Jan 10, 2018

THANK YOU

@VincSch

This comment has been minimized.

VincSch commented Jan 11, 2018

Thanks 👍

@fritsche

This comment has been minimized.

fritsche commented Jan 12, 2018

@revolter

This comment has been minimized.

revolter commented Jan 13, 2018

I get No space left on device when running sudo apt-get -f install.

@ChitteringIT

This comment has been minimized.

ChitteringIT commented Jan 16, 2018

Thanks. Worked for me!

@ajiegs

This comment has been minimized.

ajiegs commented Jan 20, 2018

Thanks.. It help me

@danbrown1010

This comment has been minimized.

danbrown1010 commented Jan 23, 2018

Worked - thx!

@ivangrafutko

This comment has been minimized.

ivangrafutko commented Jan 31, 2018

Thanks! Ran out of space. Uninstall didnt pass completly, but cleared enough space for dpkg -a --configure.

@yohancourbe

This comment has been minimized.

yohancourbe commented Feb 6, 2018

if you are lazy, you can skip 2.b with xargs and do a one line with 2.a

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

@leonelzh21

This comment has been minimized.

leonelzh21 commented Feb 9, 2018

thanks :D it worked in ubuntu 16.04 LTS

@djb3494

This comment has been minimized.

djb3494 commented Feb 9, 2018

Thank you, worked on 16.04 LTS. It would probably suit most users to have a script built in to keep no more than a few kernels. If someone knows how to get that process going, that would be a nice thing to do.

@changes165

This comment has been minimized.

changes165 commented Feb 10, 2018

thanks worked great

@stefanjauker

This comment has been minimized.

stefanjauker commented Feb 16, 2018

thanks; it worked for me!

@fionwang223

This comment has been minimized.

fionwang223 commented Mar 10, 2018

thanks; it worked for me!

@pdaoust

This comment has been minimized.

pdaoust commented Mar 14, 2018

This is brilliant; I hate having to do it by hand every time, so I'm gonna use the one-liner in a script. One issue I see though: if newer kernel versions get installed, but you haven't rebooted yet to take advantage of the new kernel, it'll eradicate those new kernels. Then, when you reboot, they won't be there anymore! 😨

@pdaoust

This comment has been minimized.

pdaoust commented Mar 14, 2018

Okay, I came up with a one-liner that only removes kernels older than the currently running one. This may not clear up enough space, unfortunately, but it won't hose the system.

dpkg -l 'linux-image*' | awk '{ if ($1=="ii") print $3}' | sort -V | perl -p -e 's/\.\d+$//' | uniq | sed "/`uname -r | sed -e 's/-generic//'`/Q" | xargs -d ' ' -i rm -rf /boot/*-{}-*
  1. Look for all linux-image packages.
  2. Get the version number if the package is installed.
  3. Sort by version number.
  4. Strip out the last number, which appears to be a package build number?
  5. Remove duplicates (caused by getting both linux-image-* and linux-image-extra-* packages).
  6. Get all kernel versions, up to but not including the currently running version.
  7. Feed that to rm via xargs.
@RuiChen96

This comment has been minimized.

RuiChen96 commented Apr 13, 2018

Thanks, it worked.

@timfrostmann

This comment has been minimized.

timfrostmann commented Apr 13, 2018

thanks, it worked for me!

@MuhamedG

This comment has been minimized.

MuhamedG commented Apr 15, 2018

thankful

@ministryofjay

This comment has been minimized.

ministryofjay commented Apr 18, 2018

Thanks! worked for me.

@HollisTech

This comment has been minimized.

HollisTech commented Apr 28, 2018

Excellent!

@peter-wangxu

This comment has been minimized.

peter-wangxu commented May 3, 2018

Worked on 16.04 as well!

@andyjlund

This comment has been minimized.

andyjlund commented May 22, 2018

Hi. I have tried the above solutions but get an error message stopping me from completing the process. I have detailed the info below:

My current version is 4.4.0-97-generic

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

linux-image-4.4.0-101-generic
linux-image-4.4.0-103-generic
linux-image-4.4.0-104-generic
linux-image-4.4.0-109-generic
linux-image-4.4.0-112-generic
linux-image-4.4.0-116-generic
linux-image-4.4.0-21-generic
linux-image-4.4.0-97-generic
linux-image-4.4.0-98-generic
linux-image-extra-4.4.0-101-generic
linux-image-extra-4.4.0-103-generic
linux-image-extra-4.4.0-104-generic
linux-image-extra-4.4.0-109-generic
linux-image-extra-4.4.0-112-generic
linux-image-extra-4.4.0-116-generic
linux-image-extra-4.4.0-21-generic
linux-image-extra-4.4.0-97-generic
linux-image-extra-4.4.0-98-generic

sudo apt-get purge linux-image-4.4.0-91-generic (or any image i choose)

Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
linux-image-extra-4.4.0-124-generic : Depends: linux-image-4.4.0-124-generic but it is not going to be installed
linux-image-extra-4.4.0-127-generic : Depends: linux-image-4.4.0-127-generic but it is not going to be installed
linux-image-extra-4.4.0-98-generic : Depends: linux-image-4.4.0-98-generic but it is not going to be installed
linux-image-generic : Depends: linux-image-4.4.0-127-generic but it is not going to be installed
Recommends: thermald but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

I've tried the apt-get -f install and it won't run (i guess down to disk space)

Can anyone give me any pointers.

Many thanks
Andy

@aspokes

This comment has been minimized.

aspokes commented May 27, 2018

Thank you soo much

@aspokes

This comment has been minimized.

aspokes commented May 27, 2018

@andyjlund Case II: Can't Use apt i.e. /boot is 100% full. I had same problem as you and it worked for me.

@desecheo

This comment has been minimized.

desecheo commented May 31, 2018

Thanks!

@tonmanna

This comment has been minimized.

tonmanna commented Jun 6, 2018

Thank you are amazing.

@vacantgeologist

This comment has been minimized.

vacantgeologist commented Jun 19, 2018

This was a huge help. For some reason none of the other information I had found was working. Thank you!

@ahbrown1

This comment has been minimized.

ahbrown1 commented Jun 19, 2018

outstanding! precisely what i was looking for. thanks.

@tvhung83

This comment has been minimized.

tvhung83 commented Jun 23, 2018

Thanks! 💯 for Case II

@suwidadi

This comment has been minimized.

suwidadi commented Jun 25, 2018

Thank you, this is definitely worked....

@RabeaWahab

This comment has been minimized.

RabeaWahab commented Jun 29, 2018

Thank you! working on 16.04.

@johnvirg

This comment has been minimized.

johnvirg commented Jul 2, 2018

Very helpful. Worked perfectly.

@dineshcooper

This comment has been minimized.

dineshcooper commented Jul 10, 2018

You are the best!

@Martlark

This comment has been minimized.

Martlark commented Jul 16, 2018

We should not be having to do this as often as I find my self doing it. Thanks for the instructions.

@hellomatthias100

This comment has been minimized.

hellomatthias100 commented Aug 15, 2018

Thanks!

@Bobspadger

This comment has been minimized.

Bobspadger commented Aug 15, 2018

superb!

@IlyaOsotov

This comment has been minimized.

IlyaOsotov commented Aug 24, 2018

Thanks, bro

@paulozullu

This comment has been minimized.

paulozullu commented Aug 28, 2018

Superb!

@BlackPanther01

This comment has been minimized.

BlackPanther01 commented Aug 30, 2018

Case 2 worked for me perfectly.

@IvanJijon

This comment has been minimized.

IvanJijon commented Sep 10, 2018

Case II worked for me in Ubuntu MATE. Kudos !

@uitk23009

This comment has been minimized.

uitk23009 commented Sep 11, 2018

Thanks!

@pjobson

This comment has been minimized.

pjobson commented Sep 24, 2018

Thanks!

@elecyb

This comment has been minimized.

elecyb commented Sep 27, 2018

Worked! :) Thank you!!

@chimdiadi

This comment has been minimized.

chimdiadi commented Sep 28, 2018

👍

@Tiagojdferreira

This comment has been minimized.

Tiagojdferreira commented Sep 28, 2018

Thanks for this, I ended up using dpkg directly to remove the old packages as apt kept refilling the partition every time I ran apt install -f!

This helped in my case. I ended up doing:
dpkg --remove linux-image-extra-4.4.0-{93,96,97,98}-generic linux-image-4.4.0-{93,96,97,98}-generic

Where I got the version from:
dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v uname -r``

@rakeshravipati

This comment has been minimized.

rakeshravipati commented Oct 11, 2018

Thank you.

@bhollan

This comment has been minimized.

bhollan commented Oct 13, 2018

Cheers to you @ipbastola for creating this, and cheers to everyone in between then and now for saying it still works.
Still works for me too. 👍

Relevant

@vaskaloidis

This comment has been minimized.

vaskaloidis commented Oct 19, 2018

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

This is an awesome command - really helpful, but there is a typo here, if you cut-and-pasted this command without checking it, (caused by the Markdown parser). The script itself is correct, but the Markdown parser hides the back-ticks around uname -r. uname -r needs to be surrounded by back-ticks ``

A extremely important tip: Never execute code you find on the internet, if you do not fully understand it first. (I learned this lesson when I was 14 and somebody in the #ubuntu IRC channel was helping me with something, and sent me a command to execute in terminal. Yup, he installed a backdoor into my system. This was 15 years ago and I am still haunted.

Read the command line-by-line until you understand how each section works. Start by executing the first command before the pipe |, then add the second, then add the third to see what data is getting piped into the next one. Then before you run it in it's entirety...

@Benzlxs

This comment has been minimized.

Benzlxs commented Oct 26, 2018

It helps me out. Many thanks to you.

@theider

This comment has been minimized.

theider commented Oct 31, 2018

Nice! Thank you so much.

@moteus

This comment has been minimized.

moteus commented Nov 14, 2018

Just small note.
In my system uname -r returns not the latest version but one behind before reboot.
Seems it need to reboot after update.

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