3

Am currently running POP_OS 20.04 (LTS). When I open the terminal and run the command,

dpkg --list | grep linux-image 

it returns a list of apparently installed images, including my current (6.0.12) and most recent (5.17.5), about nine images from version set 5.0 and six from version set 5.3 ...plus one from 4.18 and one from 5.4. console shows outdated images

Previously I have used synaptic to remove older kernels from the system, but for some reason when I search for linux-image (and sort installed images to the top) only my current and most recent are showing as installed ~ the box is green. These older 5.0/5.3 images don't show in the search results. old images do not appear in synaptic

Stacer uninstaller also cannot find them. stacer cannot find old images

Can anyone tell me why these images show in the console but not synaptic? What are they doing on the system and are they even really on the system? If they are indeed taking up space on my home partition how can I safely remove them?

One consideration comes to mind is this system started out at POP_OS version 18.10 and has been upgraded with each release up to 20.04 (LTS) -- although not sure if that has anything to do with it. Another thought is maybe this has something to do with the recovery partition, but I have no idea how or why it would need so many versions of the kernel, or why they would accumulate in such untidy manner.

2
  • 2
    While I can't speak to synaptic (didn't even know it still existed), you can remove them at the command line. sudo apt purge <package-name>. Commented Mar 27, 2023 at 23:52
  • This is just a side question, but does anyone know if the Recovery Partition in POP_OS contains a kernel? And would it ever show up in queries such as the aforementioned grep list? I don't actually know how linux recovery partitions work (or how they're structured). Commented Mar 29, 2023 at 18:09

2 Answers 2

6

Intro:

Like yourself and all other Debian & derivative users, I needed a way to prune my old Kernels. However, the ones I found on the 'net were almost perfect- they deleted all Kernels except the current ones- but unfortunately they would also remove the LATEST version that I wanted to upgrade to.

Solution:

The below one-liner-there may be more elegant ways to write it- will remove all Kernels except for both the CURRENT and the LATEST Kernels. I eat my own dog food and can say it's tested and known to work on my own inventory of Ubuntu 22.04 hosts:

Test (won't apply changes):

dpkg --list | grep -v $(uname -r) | grep -E 'linux-image-[0-9]|linux-headers-[0-9]' | awk '{print $2" "$3}' | sort -k2,2 | head -n -2 | awk '{print $1}' | xargs sudo apt-get purge 

Apply Changes (-y to the apt-get purge command above):

dpkg --list | grep -v $(uname -r) | grep -E 'linux-image-[0-9]|linux-headers-[0-9]' | awk '{print $2" "$3}' | sort -k2,2 | head -n -2 | awk '{print $1}' | xargs sudo apt-get -y purge 

How it Works:

Part 1: List all kernels & Headers EXCLUDING (grep -V) the current one:

dpkg --list | grep -v $(uname -r) | grep -E 'linux-image-[0-9]|linux-headers-[0-9]'

Part 2: Only print the column with the Kernels & headers from the dpkg --list output

dpkg --list | grep -v $(uname -r) | grep -E 'linux-image-[0-9]|linux-headers-[0-9]' | awk '{print $2" "$3}'

Part 3: Next we sort the list by the 2nd column in ascending numerical order:

dpkg --list | grep -v $(uname -r) | grep -E 'linux-image-[0-9]|linux-headers-[0-9]' | awk '{print $2" "$3}' | sort -k2,2

Part 4: Print list EXCLUDING the last (2) lines which are the Kernel & header for the LATEST Kernel version:

dpkg --list | grep -v $(uname -r) | grep -E 'linux-image-[0-9]|linux-headers-[0-9]' | awk '{print $2" "$3}' | sort -k2,2 | head -n -2

Part 5: After sorting on the 2nd column we print just the Kernel & header package names:

dpkg --list | grep -v $(uname -r) | grep -E 'linux-image-[0-9]|linux-headers-[0-9]' | awk '{print $2" "$3}' | sort -k2,2 | head -n -2 | awk '{print $1}'

Part 6: Finally, after we've built the list which excludes both the CUURRENT and LATEST Kernel versions, we feed it to xargs which chews through each line with apt-get -y purge

dpkg --list | grep -v $(uname -r) | grep -E 'linux-image-[0-9]|linux-headers-[0-9]' | awk '{print $2" "$3}' | sort -k2,2 | head -n -2 | awk '{print $1}' | xargs sudo apt-get -y purge

Conclusion:

There might be more elegant & tighter ways of writing the above, but it does the business. Anyway, hope this saves you- and others- some time-

6
  • egrep is deprecated. Use grep -E instead Commented Aug 19, 2023 at 18:39
  • And I wouldn't say that list of pipes looks elegant. awk can grep/sort itself in one shell call Commented Aug 19, 2023 at 18:43
  • @GillesQuénot Thanks for catching that; my bad. And appreciate it could be a bit more tidy as I noted in the conclusion ;-) Totally appreciate your feedback and I updated post with it- Commented Aug 20, 2023 at 0:03
  • 3
    This is the answer. Commented Dec 14, 2023 at 20:37
  • 1
    Thanks @AndreaTurbiglio I’ll take a look and update accordingly if an improvement. Thanks for your feedback- much appreciated!!!! Commented May 9, 2024 at 10:10
1

The rc marker at the start of each line indicates that the packages are removed, but configured — i.e. all their contents have been removed, apart from configuration files. Packages in this state don’t appear in Synaptic by default.

You can remove them with sudo dpkg --purge or sudo apt purge, listing the packages you want to remove. sudo apt autoremove --purge should purge them automatically without having to list them.

1
  • That rc markers indicate leftover config files is helpful to know. Thank you. On this system sudo apt autoremove --purge didn't remove these image rc entries (for reasons unknown) as it's part of my monthly maintenance routine. But was able to manually remove each one using sudo apt purge [image version]. This didn't free up any space on the drive but did wonders for my OCD. Commented Mar 29, 2023 at 18:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.