17

I've already call apt-get purge to uninstall the old kernels. But some error occured during the purge that caused the folders to be left behind. It said the folder was not empty so it couldn't delete. To free up disk space can I manually delete those folders that I've purged?

1.1M    ./4.15.0-20-generic
60M     ./4.15.0-32-generic <-- I've purged this
60M     ./4.15.0-30-generic <-- and this
236M    ./4.15.0-33-generic <-- this is my current one

Can I also remove some of these packages?

un  linux-headers-4.15.0-20-generic                       <none>                          <none>                          (no description available)
ii  linux-headers-4.15.0-30                               4.15.0-30.32                    all                             Header files related to Linux kernel version 4.15.0
ii  linux-headers-4.15.0-30-generic                       4.15.0-30.32                    amd64                           Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
ii  linux-headers-4.15.0-32                               4.15.0-32.35                    all                             Header files related to Linux kernel version 4.15.0
ii  linux-headers-4.15.0-32-generic                       4.15.0-32.35                    amd64                           Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
ii  linux-headers-4.15.0-33                               4.15.0-33.36                    all                             Header files related to Linux kernel version 4.15.0
ii  linux-headers-4.15.0-33-generic                       4.15.0-33.36                    amd64                           Linux kernel headers for version 4.15.0 on 64 bit x86 SMP
rc  linux-image-4.15.0-20-generic                         4.15.0-20.21                    amd64                           Signed kernel image generic
ii  linux-image-4.15.0-33-generic                         4.15.0-33.36                    amd64                           Signed kernel image generic
un  linux-image-unsigned-4.15.0-20-generic                <none>                          <none>                          (no description available)
un  linux-image-unsigned-4.15.0-33-generic                <none>                          <none>                          (no description available)
rc  linux-modules-4.15.0-20-generic                       4.15.0-20.21                    amd64                           Linux kernel extra modules for version 4.15.0 on 64 bit x86 SMP
ii  linux-modules-4.15.0-30-generic                       4.15.0-30.32                    amd64                           Linux kernel extra modules for version 4.15.0 on 64 bit x86 SMP
ii  linux-modules-4.15.0-32-generic                       4.15.0-32.35                    amd64                           Linux kernel extra modules for version 4.15.0 on 64 bit x86 SMP
ii  linux-modules-4.15.0-33-generic                       4.15.0-33.36                    amd64                           Linux kernel extra modules for version 4.15.0 on 64 bit x86 SMP
rc  linux-modules-extra-4.15.0-20-generic                 4.15.0-20.21                    amd64                           Linux kernel extra modules for version 4.15.0 on 64 bit x86 SMP
ii  linux-modules-extra-4.15.0-33-generic

7 Answers 7

4

Ok so I decided to just take the risk and manually delete the folder under /lib/modules and had no problem rebooting my server.

3
  • I did the same and also experienced no problem
    – gene_wood
    Sep 20, 2019 at 14:40
  • 1
    See NotoriousPyro's response. Find the installed packages with dpgk -l | grep -E 'linux-.+-[0-9]+' , and delete the older packages. Just make sure you don't delete the current version packages ( check with uname -r ) If you removed the folders manually, APT's information is somehow broken.
    – Clon
    May 8, 2021 at 19:20
  • 1
    Look for linux-image- , linux-headers- , linux-modules- , linux-modules-extra- with old version numbers
    – Clon
    May 8, 2021 at 19:36
4

You can safely remove the corresponding version of packages to the version of the kernel that you removed. In your example because you are missing the corresponding kernel versions for the following, so you can ask your package manager to also remove:

linux-headers-4.15.0-30
linux-headers-4.15.0-30-generic
linux-modules-4.15.0-30-generic
linux-headers-4.15.0-32
linux-headers-4.15.0-32-generic
linux-modules-4.15.0-32-generic
1
  • 1
    +1 because this is the right way to delete the files, through apt
    – Clon
    May 8, 2021 at 19:40
2

I had about 30 /lib/modules directories and modules from kernels no longer in use and I deleted them all freeing up over 5GB of space. I don't know why they don't get deleted when I delete the kernels with ukuu. It seems this hasn't caused a problem yet.

2

I also ran into the same predicament. As some of the others mentioned, I was also able to safely delete folders in /lib/modules. I believe the error you ran into was due to a package that couldn't install due to not enough disk space. In my case, I tried fixing the packages with apt-get -f install but ran into a No space left on device error. I was able to successfully remove the old kernels using apt-get autoremove -f. That freed up quite a bit of space. Hope this helps someone.

1

this is the method I used

after dpkg --list | grep linux-image

which returned...

rc linux-image-4.19.0-14-amd64

and

ls -l /lib/modules/

drwxr-xr-x 2 root root 4096 Apr 16 22:57 4.19.0-14-amd64

using the following command

sudo dpkg --purge linux-image-4.19.0-14-amd64

removed the folder under /lib/modules as well as package and install files located under /var/lib/dpkg/info/

2
  • if yoou do so, and its your current kernel you will kill your system, thats a dangerouse action
    – djdomi
    Aug 9, 2021 at 10:57
  • of course it's not my current kernel... I am using 5.10.46-4-amd64 on Bullseye there were a few more entries from way back that I removed this way too... there from when I was on Buster...I was only using 4.19.0-14-amd64 as an example this method, using dpkg --purge is a more advisable and efficient solution than manually deleting folders
    – oddyolynx
    Aug 10, 2021 at 6:56
0

I know it's not Ubuntu, but in CentOS the "current version" of the kernel I guess resides there so you can't delete all of them.

Nor is it safe to manually delete "old ones" in case you were in the middle of installing a new one but the transaction hadn't succeeded. You won't know exactly which one to delete.

yum autoremove doesn't seem to remove old ones for whatever reason. It's even recommended to keep two versions around in case you need an old one: https://www.golinuxcloud.com/remove-old-kernels-rhel-centos-8/#Best_practices_to_remove_old_kernels

You can manually remove packages for the older ones:

https://www.nixcraft.com/t/can-i-delete-lib-modules-directory-in-linux-safely/1934/2

So the take home lesson is "only delete kernels that are older" (see above link describing how to check) and "prefer deleting them via removing their package" to manually nuking their directories.

Also note that if you do accidentally remove the "current kernel" you may be able to recover if you happen to have a few older kernels still kicking around.

0

The correct way to remove those is to uninstall unused linux-image- and linux-headers- packages.

# apt list --installed | grep -P "linux-(image|headers)-"
# apt remove linux-image-5.10.0-10-amd64
# apt remove linux-headers-5.10.0-10-amd64

But if you have manually-installed DKMS modules, uninstalling linux-image- and linux-headers- packages doesn't clean those up, which leads to subfolders in /lib/modules/ remaining.

Run dkms status to list installed DKMS modules (or run ls -l /var/lib/dkms/*/)

Remove modules pointing to uninstalled kernels using dkms remove:

# dkms status
acpi-call, 1.1.0, 5.10.0-10-amd64, x86_64: installed
acpi-call, 1.1.0, 5.10.0-13-amd64, x86_64: installed
acpi-call, 1.1.0, 5.10.0-14-amd64, x86_64: installed

# dkms remove acpi-call/1.1.0 -k 5.10.0-10-amd64

acpi_call.ko:
 - Uninstallation
   - Deleting from: /lib/modules/5.10.0-10-amd64/

# dkms remove acpi-call/1.1.0 -k 5.10.0-13-amd64

acpi_call.ko:
 - Uninstallation
   - Deleting from: /lib/modules/5.10.0-13-amd64/

# 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .