75

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...

Ryko
  • 1,155
  • 1
    Yeah sure!! I have edited the question... – Ryko May 16 '18 at 08:47
  • 1
    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 08: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 04:41
  • 1
    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 04:44
  • Old snaps are automatically removed, so that there are a max of three. The discussion noted by @popey is about making the number configurable. This work, to allow the default to be lowered to 2 revisions per snap (but no lower), seems to be merged now: https://github.com/snapcore/snapd/pull/5207 Any hope of getting it backported to Bionic 18.04 LTS? Allowing the number of saved revisions to be configurable per snap would also be good for those huge packages out there. – nealmcb Oct 30 '18 at 11:55
  • 1
    This is being worked on by a snapd developer now: https://github.com/snapcore/snapd/pull/5207 – Zygmunt Krynicki May 25 '18 at 14:53
  • This work, to allow the default to be lowered to 2 revisions per snap, seems to be merged now. Any hope of getting it in Bionic 18.04 LTS? – nealmcb Oct 30 '18 at 11:53

9 Answers9

60

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

#!/bin/sh
set -eu

LANG=C 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.

Chipaca
  • 9,990
52

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

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

You can also free some space by removing snap's cache:

sudo du -sh /var/lib/snapd/cache/                  # Get used space
sudo find /var/lib/snapd/cache/ -exec rm -v {} \;  # Remove cache

Related

Pablo Bianchi
  • 15,657
  • As /var/lib/snapd/cache/ is not readable by the normal user, so asterisk does not expand to the files. Hence, I did sudo su first and then rm --force /var/lib/snapd/cache/* – Edvard Rejthar Nov 26 '22 at 16:09
  • The default retain value is 3 and you can see if you have overwritten it with sudo snap get system refresh.retain. – Akaisteph7 Jan 03 '24 at 17:09
9

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

LANG=C snap list --all | awk '/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.

Pablo Lalloni
  • 191
  • 1
  • 2
5

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

keturn
  • 1,257
3

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.

popey
  • 23,667
  • 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 08: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
3

just another option to the list, using parallel

snap list --all | awk '/disabled/{printf "snap remove %s --revision=%i \n",$1,$3}' | parallel

removed all disabled snaps in my system

core18 (revision 1705) removed
code (revision 33) removed
core (revision 9066) removed
gnome-3-34-1804 (revision 33) removed
powershell (revision 131) removed
snap-store (revision 433) removed
snapd (revision 7264) removed
rpi-imager (revision 28) removed
RASG
  • 246
2

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.

1

I just did this:

  • List all unused/disabled snap packages:

    sudo snap list | grep disabled

  • Copy & paste the output into a spreadsheet

Usually, the output is in one column. Just do Find & Replace, repeatedly replace double spaces by single spaces until you have only single spaces left.

  • Split your column into several columns using the single space as a delimiter. Every spreadsheet has such a tool. In LibreOffice, it's Data --> Text to Columns.

  • Remove the columns you don't need anymore.

  • Add some columns that hold sudo snap remove and --revision

My ready-to-use commands, in my spreadsheet

  • Just copy & paste your stuff back to the console.
user258532
  • 1,258
  • 2
  • 16
  • 25
0

Building on @casper.dcl answer because I'm running ubuntu 20.04

snap list --all | awk '$6~"disabled"{print $1" --revision "$3}'

builds the correct list to pipe into xargs

$ ddf -m
Filesystem                 Type 1M-blocks  Used Available Use% Mounted on
/dev/mapper/vgubuntu-root  ext4     29170 26363      1358  96% /

$ snap list --all | awk '$6~"disabled"{print $1" --revision "$3}' chromium --revision 2465 core18 --revision 2745 core20 --revision 1879 cups --revision 836 gnome-3-28-1804 --revision 194 gnome-3-34-1804 --revision 90 gnome-3-38-2004 --revision 137 gtk-common-themes --revision 1534 snap-store --revision 638 snapd --revision 19122

finally the actual command

$ snap list --all | awk '$6~"disabled"{print $1" --revision "$3}' | sudo xargs -rn3 snap remove chromium (revision 2465) removed core18 (revision 2745) removed core20 (revision 1879) removed cups (revision 836) removed gnome-3-28-1804 (revision 194) removed gnome-3-34-1804 (revision 90) removed gnome-3-38-2004 (revision 137) removed gtk-common-themes (revision 1534) removed snap-store (revision 638) removed snapd (revision 19122) removed

$ ddf -m Filesystem Type 1M-blocks Used Available Use% Mounted on /dev/mapper/vgubuntu-root ext4 29170 25174 2547 91% /

1.2G regained.

X Tian
  • 240
  • 2
  • 8