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

7
  • 1
    Yeah sure!! I have edited the question...
    – Ryko
    May 16, 2018 at 8: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, 2018 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, 2018 at 4: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, 2018 at 4:44
  • 1
    This is being worked on by a snapd developer now: github.com/snapcore/snapd/pull/5207 May 25, 2018 at 14:53

9 Answers 9

61

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.

12
  • I would prepend a sudo to the snap commands. Feb 6, 2019 at 9:50
  • 12
    no, don't do that: sudo should be done by the caller, explicitly.
    – Chipaca
    Mar 19, 2019 at 14:42
  • 3
    You can use xargs to make it a oneliner :)
    – Ctrl-C
    Mar 22, 2019 at 16:13
  • 1
    To really free up space. You can safely remove the cache also with sudo rm /var/lib/snapd/cache/*
    – rubo77
    Jan 13, 2020 at 19:28
  • 1
    @bcag2 the LANG=C part will temporarily switch the language to english, so you don't need to touch the code. I tried with Czech, just now.
    – jena
    Apr 4, 2023 at 16:43
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

2
  • 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/* Nov 26, 2022 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 3 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.

4
  • 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, 2019 at 16:28
  • 7
    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, 2019 at 11:45
  • @MikeHicks You could file the bug here May 1, 2019 at 16:46
  • Why LANG=C is needed?
    – mario
    Jun 29, 2023 at 16:31
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

1
  • Nice work with JSON format!
    – Binar Web
    Sep 13, 2021 at 16:44
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.

2
  • 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, 2018 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, 2018 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
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.

3
  • I do not get it Mar 29, 2019 at 16:20
  • You can avoid the error using xarg's -r flag. Mar 31, 2019 at 15:01
  • Problem is that command could remove snaps with "disabled" in its name... unlikely but... Mar 31, 2019 at 15:02
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.
1
  • A speadsheet? really?
    – harmv
    Aug 23, 2023 at 9:36
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .