19

When I run snap list --all command it shows all the installed versions of snap packages.

Name                  Version       Rev   Tracking  Developer     Notes
atom                  1.26.1        150   stable    snapcrafters  classic
brave                 v0.22.669dev  23    stable    brave         -
core                  16-2.32.6     4571  stable    canonical     core
core                  16-2.32.5     4486  stable    canonical     core,disabled
firefox               59.0.2-1      71    stable    mozilla       disabled
firefox               60.0-2        85    stable    mozilla       -
gimp                  2.10.0        38    stable    snapcrafters  disabled
gimp                  2.10.0        39    stable    snapcrafters  -
gimp                  2.8.22        30    stable    snapcrafters  disabled
gnome-3-26-1604       3.26.0        64    stable/…  canonical     -
gnome-3-26-1604       3.26.0        62    stable/…  canonical     disabled
gnome-characters      3.28.0        86    stable/…  canonical     -
gnome-characters      3.26.2        69    stable/…  canonical     disabled
gnome-logs            3.26.2        25    stable/…  canonical     disabled
gnome-logs            3.28.0        31    stable/…  canonical     -
gnome-system-monitor  3.26.0        36    stable/…  canonical     disabled
gnome-system-monitor  3.26.0        39    stable/…  canonical     -
libreoffice           6.0.3.2       59    stable    canonical     disabled
libreoffice           6.0.4.2       63    stable    canonical     -

Now there are so many disabled packages there which are no longer used. So I want to remove those packages. Here the command is snap remove gnome-3-26-1604 --revision=62 . Now I have to do it for all the revisions and for all the packages one by one. Is there any command which will remove the disabled or unused snap packages with single line of command?

Thanks in advance...

  • 1
    Yeah sure!! I have edited the question... – Ryko May 16 '18 at 8:47
  • Note that keeping a single previous version is by design: when a snap is updated, the older revision will be removed. – muru May 16 '18 at 8:55
  • But here gimp has 3 versions (revision-30,38,39). Though revision 38 and 39 has same versions... I will inform you later when any of this apps get further updates... – Ryko May 17 '18 at 4:41
  • Is there any settings available in snap so that I could disable keeping previous versions of the snap applications... Because some of them requires so much space... like libre-office takes in nearly 2GB of space, and if the previous version is included it becomes 4GB. – Ryko May 17 '18 at 4:44
  • This is being worked on by a snapd developer now: github.com/snapcore/snapd/pull/5207 – Zygmunt Krynicki May 25 '18 at 14:53
22

I'll look into adding this sometime soon (as a 20% thing). Meanwhile, you could drop

#!/bin/sh
set -eu

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

into a shell script and run that.

|improve this answer|||||
  • I would prepend a sudo to the snap commands. – Pierre François Feb 6 '19 at 9:50
  • 1
    no, don't do that: sudo should be done by the caller, explicitly. – Chipaca Mar 19 '19 at 14:42
  • furthermore, you don't necessarily need sudo for snap remove (if you did a snap login) – Chipaca Mar 19 '19 at 14:42
  • 1
    You can use xargs to make it a oneliner :) – Ctrl-C Mar 22 '19 at 16:13
  • @Ctrl-C I say go for it – Chipaca Mar 23 '19 at 18:00
4

I found more elegant and easy to use on an alias (using single quotes) this solution:

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

Starting from snap 2.34 and later, you can set the maximum number of a snap’s revisions stored by the system by setting a refresh.retain option (source).

sudo snap set system refresh.retain=2 
|improve this answer|||||
2

The capability to purge/remove old/disabled snaps has been discussed previously but not yet implemented. In the meantime unfortunately it's a manual process.

|improve this answer|||||
  • 1
    Is there anyway to get machine-readable information about snaps (JSON or similar) that can be easily parsed to automate this? – muru May 25 '18 at 8:23
  • 1
    It looks like the discussion actually notes that old snaps are automatically removed, so that there are a max of three. The discussion is about making the number configurable. – nealmcb Oct 30 '18 at 11:52
1

Building on previous answers, you should probably be doing something like:

snap list --all | awk '$5~"disabled"{print $1" --revision "$3}' | xargs -rn3 snap remove

Which avoids emitting an error when there's no disabled snap revision and looks for "disabled" specifically in the notes column.

|improve this answer|||||
  • oh hah I edited my answer based on your comments on it, but just saw that you've added a basically identical answer :) – casper.dcl Apr 1 '19 at 16:28
  • 4
    Unfortunately in my version of snap (2.38+18.10 on Ubuntu 18.10), the Notes column is $6 rather than $5. The snap list command really needs a --format option so that output can be consistent for scripting. – Mike Hicks Apr 18 '19 at 11:45
  • @MikeHicks You could file the bug here – Pablo Bianchi May 1 '19 at 16:46
1

After seeing the note from Mike about these scripts breaking between versions because the number of output columns changed, I went looking to see if there are any machine-readable bindings to this stuff.

What I found was snapd's REST API.

The equivalent of snap list --all is

curl --silent --show-error \
    --get --data select=all \
    --unix-socket /run/snapd.socket \
    http://localhost/v2/snaps

It returns JSON. To filter it down to non-active packages and turn it in to a list of snap remove commands, you can use jq:

jq --raw-output '
    .result[] | 
    select(.status != "active") | 
    "snap remove --revision=\(.revision|@sh) \(.name|@sh)"
'

You technically could run all that on a single line (the breaks I inserted are for readability), but it's quite a handful, so I wrapped it in a script: remove-disabled-snaps.sh

|improve this answer|||||
0

Building on @Chipaca, @Ctrl-C and @Pablo Lalloni's suggestions:

snap list --all | awk '$5~"disabled"{print $1" --revision "$3}' | xargs -rn3 snap remove

This searches for "disabled" in the "Notes" column of snap list --all and runs snap remove <snap> --revision <rev> of the corresponding snap.

|improve this answer|||||
  • I do not get it – Pierre.Vriens Mar 29 '19 at 16:20
  • You can avoid the error using xarg's -r flag. – Pablo Lalloni Mar 31 '19 at 15:01
  • Problem is that command could remove snaps with "disabled" in its name... unlikely but... – Pablo Lalloni Mar 31 '19 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.