Create a gist now

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.

Show comment
Hide comment
@michaelschem

michaelschem Sep 8, 2017

For an all in one try this:

sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -vuname -r| while read -r line; do sudo apt-get -y purge $line;done;sudo apt-get autoremove; sudo update-grub

For an all in one try this:

sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -vuname -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.

Show comment
Hide comment
@GuillermoBlasco

GuillermoBlasco Sep 17, 2017

Working on 16.04 LTS as wll

Working on 16.04 LTS as wll

@rico

This comment has been minimized.

Show comment
Hide comment
@rico

rico Sep 20, 2017

Works, thanks.

rico commented Sep 20, 2017

Works, thanks.

@amitsaha

This comment has been minimized.

Show comment
Hide comment
@amitsaha

amitsaha Nov 1, 2017

THANK YOU

amitsaha commented Nov 1, 2017

THANK YOU

@jigewxy

This comment has been minimized.

Show comment
Hide comment
@jigewxy

jigewxy Nov 4, 2017

thanks, it worked perfectly.

jigewxy commented Nov 4, 2017

thanks, it worked perfectly.

@eggfriedrice

This comment has been minimized.

Show comment
Hide comment
@eggfriedrice

eggfriedrice 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!

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.

Show comment
Hide comment

cptx86 commented Dec 11, 2017

Thanks!

@sametserpil

This comment has been minimized.

Show comment
Hide comment

Thanks

@mwanamziki

This comment has been minimized.

Show comment
Hide comment
@mwanamziki

mwanamziki Jan 8, 2018

Thanks, this has been of great help to me

Thanks, this has been of great help to me

@garrett-eclipse

This comment has been minimized.

Show comment
Hide comment

THANK YOU

@VincSch

This comment has been minimized.

Show comment
Hide comment
@VincSch

VincSch Jan 11, 2018

Thanks 👍

VincSch commented Jan 11, 2018

Thanks 👍

@fritsche

This comment has been minimized.

Show comment
Hide comment
@revolter

This comment has been minimized.

Show comment
Hide comment
@revolter

revolter Jan 13, 2018

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

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

@ChitteringIT

This comment has been minimized.

Show comment
Hide comment
@ChitteringIT

ChitteringIT Jan 16, 2018

Thanks. Worked for me!

Thanks. Worked for me!

@ajiegs

This comment has been minimized.

Show comment
Hide comment
@ajiegs

ajiegs Jan 20, 2018

Thanks.. It help me

ajiegs commented Jan 20, 2018

Thanks.. It help me

@danbrown1010

This comment has been minimized.

Show comment
Hide comment
@danbrown1010

danbrown1010 Jan 23, 2018

Worked - thx!

Worked - thx!

@ivangrafutko

This comment has been minimized.

Show comment
Hide comment
@ivangrafutko

ivangrafutko Jan 31, 2018

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

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

@yohancourbe

This comment has been minimized.

Show comment
Hide comment
@yohancourbe

yohancourbe 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

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.

Show comment
Hide comment
@leonelzh21

leonelzh21 Feb 9, 2018

thanks :D it worked in ubuntu 16.04 LTS

thanks :D it worked in ubuntu 16.04 LTS

@djb3494

This comment has been minimized.

Show comment
Hide comment
@djb3494

djb3494 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.

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.

Show comment
Hide comment
@changes165

changes165 Feb 10, 2018

thanks worked great

thanks worked great

@stefanjauker

This comment has been minimized.

Show comment
Hide comment
@stefanjauker

stefanjauker Feb 16, 2018

thanks; it worked for me!

thanks; it worked for me!

@fionwang223

This comment has been minimized.

Show comment
Hide comment
@fionwang223

fionwang223 Mar 10, 2018

thanks; it worked for me!

thanks; it worked for me!

@pdaoust

This comment has been minimized.

Show comment
Hide comment
@pdaoust

pdaoust 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 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.

Show comment
Hide comment
@pdaoust

pdaoust 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.

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.

Show comment
Hide comment
@RuiChen96

RuiChen96 Apr 13, 2018

Thanks, it worked.

Thanks, it worked.

@timfrostmann

This comment has been minimized.

Show comment
Hide comment
@timfrostmann

timfrostmann Apr 13, 2018

thanks, it worked for me!

thanks, it worked for me!

@AmohamedO

This comment has been minimized.

Show comment
Hide comment

thankful

@ministryofjay

This comment has been minimized.

Show comment
Hide comment
@ministryofjay

ministryofjay Apr 18, 2018

Thanks! worked for me.

Thanks! worked for me.

@HollisTech

This comment has been minimized.

Show comment
Hide comment

Excellent!

@peter-wangxu

This comment has been minimized.

Show comment
Hide comment
@peter-wangxu

peter-wangxu May 3, 2018

Worked on 16.04 as well!

Worked on 16.04 as well!

@andyjlund

This comment has been minimized.

Show comment
Hide comment
@andyjlund

andyjlund 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

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.

Show comment
Hide comment
@aspokes

aspokes May 27, 2018

Thank you soo much

aspokes commented May 27, 2018

Thank you soo much

@aspokes

This comment has been minimized.

Show comment
Hide comment
@aspokes

aspokes 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.

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.

Show comment
Hide comment

Thanks!

@tonmanna

This comment has been minimized.

Show comment
Hide comment
@tonmanna

tonmanna Jun 6, 2018

Thank you are amazing.

tonmanna commented Jun 6, 2018

Thank you are amazing.

@vacantgeologist

This comment has been minimized.

Show comment
Hide comment
@vacantgeologist

vacantgeologist Jun 19, 2018

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

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.

Show comment
Hide comment
@ahbrown1

ahbrown1 Jun 19, 2018

outstanding! precisely what i was looking for. thanks.

outstanding! precisely what i was looking for. thanks.

@tvhung83

This comment has been minimized.

Show comment
Hide comment
@tvhung83

tvhung83 Jun 23, 2018

Thanks! 💯 for Case II

tvhung83 commented Jun 23, 2018

Thanks! 💯 for Case II

@suwidadi

This comment has been minimized.

Show comment
Hide comment
@suwidadi

suwidadi Jun 25, 2018

Thank you, this is definitely worked....

Thank you, this is definitely worked....

@RabeaWahab

This comment has been minimized.

Show comment
Hide comment
@RabeaWahab

RabeaWahab Jun 29, 2018

Thank you! working on 16.04.

Thank you! working on 16.04.

@johnvirg

This comment has been minimized.

Show comment
Hide comment
@johnvirg

johnvirg Jul 2, 2018

Very helpful. Worked perfectly.

johnvirg commented Jul 2, 2018

Very helpful. Worked perfectly.

@dineshcooper

This comment has been minimized.

Show comment
Hide comment
@dineshcooper

dineshcooper Jul 10, 2018

You are the best!

You are the best!

@Martlark

This comment has been minimized.

Show comment
Hide comment
@Martlark

Martlark Jul 16, 2018

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

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

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