The Computer Oracle

How to remove old version of installed snaps

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001

--

Chapters
00:00 Question
00:46 Accepted answer (Score 188)
01:19 Answer 2 (Score 90)
01:43 Answer 3 (Score 89)
02:10 Answer 4 (Score 39)
03:11 Thank you

--

Full question
https://superuser.com/questions/1310825/...

Answer 1 links:
[script from another answer]: https://superuser.com/a/1330590/185554

Answer 3 links:
[snapd docs on versions]: https://docs.snapcraft.io/core/versions

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux

#avk47



ACCEPTED ANSWER

Score 217


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

The "Close snaps" is there because you may not have restarted an application before you updated. So it's possible you're actually running a revision which is to be removed by the script.




ANSWER 2

Score 119


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.




ANSWER 3

Score 103


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 



ANSWER 4

Score 42


The snapd docs on versions state that the outdated revisions should be automatically removed so that no more than the last two revisions are installed. However, I also saw more than two versions of my snaps installed.

You can list all the revisions with snap list --all to see something like:

Name     Version                  Rev   Tracking  Developer  Notes
core     16-2.31.2                4206  stable    canonical  core,disabled
core     16-2.32.3                4407  stable    canonical  core,disabled
core     16-2.32.5                4486  stable    canonical  core
spotify  1.0.70.399.g5ffabd56-26  5     stable    spotify    disabled
spotify  1.0.72.117.g6bd7cc73-35  6     stable    spotify    disabled
spotify  1.0.77.338.g758ebd78-41  13    stable    spotify    -

You can remove individual revisions with

snap remove spotify --revision=5

This is safe even for the disabled revisions of core and other dependencies, and snap remove with an explicit --revision=... even prevents you from removing non-disabled snaps.