Snap does not delete all older versions of snaps

I have noticed that a third of my root directory space is overtaken by snaps, so I thought I would like to clean the older versions. The problem seems to lie specifically within skype and slack snaps.
If I list the contents of /var/lib/snapd/snaps folder, I see

-rw------- 1 root root 146771968 17. Apr 2023  skype_266.snap
-rw------- 1 root root 146771968 23. Apr 2023  skype_267.snap
-rw------- 1 root root 147140608 25. Apr 2023  skype_269.snap
-rw------- 1 root root 147464192  2. Jun 2023  skype_277.snap
-rw------- 1 root root 148238336 30. Jun 2023  skype_284.snap
-rw------- 1 root root 189390848 12. Aug 2023  skype_291.snap
-rw------- 1 root root 183984128 27. Sep 2023  skype_299.snap
-rw------- 1 root root 183963648  1. Okt 2023  skype_301.snap
-rw------- 1 root root 183963648  5. Okt 2023  skype_302.snap
-rw------- 1 root root 184414208 18. Okt 2023  skype_305.snap
-rw------- 1 root root 184414208 24. Okt 2023  skype_306.snap
-rw------- 1 root root 184573952 28. Nov 2023  skype_311.snap
-rw------- 1 root root 187752448  6. Feb 13:55 skype_326.snap
-rw------- 1 root root 186470400 21. Apr 14:28 skype_342.snap
-rw------- 1 root root 186773504  7. Mai 11:35 skype_345.snap
-rw------- 1 root root 185835520  9. Mai 12:08 skype_348.snap
-rw------- 1 root root 123162624  6. Nov 2023  slack_110.snap
-rw------- 1 root root 123101184 23. Apr 18:10 slack_142.snap
-rw------- 1 root root 123695104  4. Jun 11:01 slack_149.snap
-rw------- 1 root root 125300736 19. Jun 19:54 slack_153.snap
-rw------- 1 root root 125874176  4. Mai 2023  slack_69.snap
-rw------- 1 root root 118751232 12. Aug 2023  slack_83.snap
-rw------- 1 root root 121425920 18. Sep 2023  slack_95.snap

Namely, 16 different versions of skype and 7 versions of slack.

I thought I could use the tools provided by snap to clean the directory. So, I have tried to impose a general limit on the number of revisions with

sudo snap set system refresh.retain=2

to no avail. I have also tried to delete older versions manually

snap remove skype --revision=n

where I substituted n>=2. In the latter case, snaps answer was that no such revision is installed.

If I list all the snaps with snap list --all, only two versions are listed for skype and slack:

skype              8.117.0.202                 342    latest/stable  skype✓      disabled
skype              8.119.0.201                 348    latest/stable  skype✓      -
slack              4.39.88                     153    latest/stable  slack✓      -
slack              4.38.125                    149    latest/stable  slack✓      disabled

So it seems that snap simply does not see all the older versions of snaps.

How do I deal with this problem in a systematic manner? Is it safe to delete the folders in snap folder myself?

Hi @Gregstrq,

See:

Edit:

Also see:

https://askubuntu.com/a/1156686

Edit #2:

Also:

Also note that snap does not only keep removed snaps, but also up to 20 older versions of that snap (standard is 3 versions). So for me, cleaning up those remaining copies resulted in far more reclaimed storage than cleaning the cache (5GB vs 1GB). This website has a nice script which I used for that:

#!/bin/bash
#Removes old revisions of snaps
#CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu
LANG=en_US.UTF-8 snap list --all | awk '/disabled/{print $1, $3}' |
   while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
    done

This script wouldn’t work. As I have written, snap list --all shows only the current version and the previous revision, although there are for example 16 versions of skype appearing in /var/lib/snapd/snaps folder. And again, as I have written, I have tried to run snap remove "$snapname" --revision="$revision" manually (it is what the script does), and it didn’t work.

My first edit says:

You can remove the files in /var/lib/snapd/cache without issue. Also there is no need to stop snapd before.
[…]
Here the command to do that:

sudo sh -c 'rm -rf /var/lib/snapd/cache/*'