Dyego Eugenio/Remove old snaps from ubuntu

Created Sun, 29 Jul 2018 21:44:00 +0300 Modified Sun, 26 Jun 2022 17:51:04 +0000

Snaps are applications packaged with all their dependencies to run on all popular Linux distributions from a single build. They update automatically and roll back gracefully. Whether you’re building for desktop, cloud, or the Internet of Things, publishing as a snap will keep users up to date and make system configuration issues less likely, freeing you to code more and debug less. From Ubuntu

Discover installed snaps

Sometimes you can see a lot of loop devices mounted on your linux

[email protected]:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
...
/dev/loop3       22M   22M     0 100% /snap/gnome-logs/31
/dev/loop1      3,8M  3,8M     0 100% /snap/gnome-system-monitor/45
/dev/loop2      3,8M  3,8M     0 100% /snap/gnome-system-monitor/51
/dev/loop0      141M  141M     0 100% /snap/gnome-3-26-1604/59
/dev/loop4      2,4M  2,4M     0 100% /snap/gnome-calculator/170
/dev/loop5       15M   15M     0 100% /snap/gnome-logs/37
/dev/loop7       87M   87M     0 100% /snap/core/4650
/dev/loop6      233M  233M     0 100% /snap/pycharm-community/62
/dev/loop10     163M  163M     0 100% /snap/spotify/16
/dev/loop11     5,0M  5,0M     0 100% /snap/canonical-livepatch/39
/dev/loop8       13M   13M     0 100% /snap/gnome-characters/101
/dev/loop9      141M  141M     0 100% /snap/gnome-3-26-1604/70
/dev/loop12      87M   87M     0 100% /snap/core/4830
/dev/loop13     140M  140M     0 100% /snap/gnome-3-26-1604/64
/dev/loop14      13M   13M     0 100% /snap/gnome-characters/103
/dev/loop16     5,0M  5,0M     0 100% /snap/canonical-livepatch/41
/dev/loop15      87M   87M     0 100% /snap/core/4917
/dev/loop17     237M  237M     0 100% /snap/pycharm-community/64
/dev/loop18     2,4M  2,4M     0 100% /snap/gnome-calculator/178
/dev/sda2        96M   37M   60M  38% /boot/efi
/dev/loop20     2,4M  2,4M     0 100% /snap/gnome-calculator/180
/dev/loop19      13M   13M     0 100% /snap/gnome-characters/96
/dev/loop21     227M  227M     0 100% /snap/pycharm-community/74
/dev/loop22     3,8M  3,8M     0 100% /snap/gnome-system-monitor/41
/dev/loop23      15M   15M     0 100% /snap/gnome-logs/34
/dev/loop24      35M   35M     0 100% /snap/gtk-common-themes/319
...

On this case, you can remove that old snaps. First of all, you should list the snaps installed and than remove it:

[email protected]:~$ snap list --all
Name                  Version                 Rev   Tracking  Publisher  Notes
canonical-livepatch   8.0.1                   39    stable    canonical  disabled
canonical-livepatch   8.0.2                   41    stable    canonical  -
core                  16-2.33                 4830  stable    canonical  core,disabled
core                  16-2.32.8               4650  stable    canonical  core,disabled
core                  16-2.33.1               4917  stable    canonical  core
gnome-3-26-1604       3.26.0                  59    stable/…  canonical  disabled
gnome-3-26-1604       3.26.0                  70    stable/…  canonical  -
gnome-3-26-1604       3.26.0                  64    stable/…  canonical  disabled
gnome-calculator      3.28.1                  178   stable/…  canonical  disabled
gnome-calculator      3.28.2                  180   stable/…  canonical  -
gnome-calculator      3.28.1                  170   stable/…  canonical  disabled
gnome-characters      3.28.2                  96    stable/…  canonical  disabled
gnome-characters      3.28.2                  103   stable/…  canonical  -
gnome-characters      3.28.2                  101   stable/…  canonical  disabled
gnome-logs            3.28.2                  34    stable/…  canonical  disabled
gnome-logs            3.28.0                  31    stable/…  canonical  disabled
gnome-logs            3.28.2                  37    stable/…  canonical  -
gnome-system-monitor  3.28.2                  51    stable/…  canonical  -
gnome-system-monitor  3.26.0                  41    stable/…  canonical  disabled
gnome-system-monitor  3.26.0                  45    stable/…  canonical  disabled
gtk-common-themes     0.1                     319   stable    canonical  -
pycharm-community     2018.1.3                62    stable    jetbrains  disabled,classic
pycharm-community     2018.1.4                64    stable    jetbrains  disabled,classic
pycharm-community     2018.2                  74    stable    jetbrains  classic
spotify               1.0.80.474.gef6b503e-7  16    stable    spotify    -

Remove one-by-one

We have some disabled snaps and these could be removed.

[email protected]:~$ sudo snap remove canonical-livepatch --revision=39
canonical-livepatch removed

Remove like a boss

But, I guess it is a quite annoying to do one by one, isn’t it? However, we have in our favor a super super tool: Bash!

[email protected]:~$ for i in $(snap list --all |grep disabled | awk '{print $1";"$3}'); do sudo snap remove ${i%;*} --revision ${i#*;}; done
core removed
core removed
gnome-3-26-1604 removed
gnome-3-26-1604 removed
gnome-calculator removed
gnome-calculator removed
gnome-characters removed
gnome-characters removed
gnome-logs removed
gnome-logs removed
gnome-system-monitor removed
gnome-system-monitor removed
pycharm-community removed
pycharm-community removed

Understand the magix

The trick here is using bash substring. Instead to use cut or awk again, and use the same loop, this command create a list with name;revision.

canonical-livepatch;39
core;4830
core;4650
gnome-3-26-1604;59
gnome-3-26-1604;64
gnome-calculator;178
gnome-calculator;170
gnome-characters;96
gnome-characters;101
gnome-logs;34
gnome-logs;31
gnome-system-monitor;41
gnome-system-monitor;45
pycharm-community;62
pycharm-community;64

Than, I use the substring to take the each portion from i. First: ${i%;*}. It’ll remove shortest match from the ; until the end of string *.

${string%substring}
Deletes shortest match of $substring from back of $string.

Reference

In the other hand, ${i#*;} will remove shortest match from the begin * until ;.

${string#substring}
Deletes shortest match of $substring from front of $string.

Reference