How to remove old version of installed snaps

I'm newbiew with snap usage, I have few apps installed on my system, something that I notice when run the command df -h I found mounted different versions of the same snap

/dev/loop0       143M   143M     0 100% /var/lib/snapd/snap/gravit-designer/7
/dev/loop1        82M    82M     0 100% /var/lib/snapd/snap/core/4110
/dev/loop7       198M   198M     0 100% /var/lib/snapd/snap/polarr/3
/dev/loop2        82M    82M     0 100% /var/lib/snapd/snap/core/4206
/dev/loop3       143M   143M     0 100% /var/lib/snapd/snap/gravit-designer/6
/dev/loop10      137M   137M     0 100% /var/lib/snapd/snap/gravit-designer/5

my question is why they keep there, the only way I found to remove the old is remove and install again the snap, there's something like prune to maintain my system?


Solution 1:

Here's a short script which will remove all old versions of snaps. This will only keep the current active version, which should recover you some disk space:

#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu

LANG=C snap list --all | awk '/disabled/{print $1, $3}' |
    while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
    done

Solution 2:

Starting from snap v2.34 and later, you can set the maximum number of snap revisions stored for each package by setting the refresh.retain option—it can only be a number between 2 and 20 and has a default value of 3.

sudo snap set system refresh.retain=2 

Solution 3:

A version of the script from another answer, as a one-liner, without the awk dependency:

# snap list --all | while read snapname ver rev trk pub notes; do if [[ $notes = *disabled* ]]; then snap remove "$snapname" --revision="$rev"; fi; done

This likely requires bash or a compatible shell with the [[ construct.