How to remove old linux kernels

Asked by Peter

I am running out of root partition space. What is an easy way of removing old kernels 2.6.24-19 to 2.6.24-22 ?

I am on 2.6.24-23 now.
Thanks
Peter

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Mark Rijckenberg
Solved:
Last query:
Last reply:
Revision history for this message
Bernhard (b.a.koenig) said :
#1

Usually you can do

sudo apt-get autoclean

and it will delete your old kernels (actually all outdated packages). Not sure if this frees enough disk space for you but you can try.

Revision history for this message
Best Mark Rijckenberg (markrijckenberg) said :
#2

Hi,

I created a Linux bash script called removekernel

Here are the contents of the removekernel script:

#!/bin/ksh

if [[ $1 == "" ]]; then
echo "No argument added after removekernel command"
echo "Please enter kernelversion to remove from your pc (for example: 2.6.24-16) "
read KERNELVERSION

echo "Removing kernelversion $KERNELVERSION"
apt-cache search $KERNELVERSION|cut -d" " -f1|xargs sudo apt-get remove -y

else
echo "Removing kernelversion $1"
apt-cache search $1|cut -d" " -f1|xargs sudo apt-get remove -y
fi

The script above will allow you to remove previous kernel versions with ease.

After making the script removekernel executable, you can use it like this in the Terminal:

removekernel 2.6.24-19

to delete the 2.6.24-19 kernel.

Watch out though: if you use the following command and you do not have any 2.6.27 kernels installed, then you wipe out ALL available Linux kernels:

removekernel 2.6.24

is VERY dangerous :-)

Regards,

Mark

Revision history for this message
Peter (nitep) said :
#3

Thanks Mark,

That did the job.
sudo apt-get clean removes the archives
sudo apt-get autoremove and sudo apt-get autoclean did not remove old archives but your script did the job nicely. I removed kernels 16 to 22 and the partition went from 88% full to 63% full.

Regards,
Peter

Revision history for this message
Peter (nitep) said :
#4

Thanks Mark Rijckenberg, that solved my question.