How to clean old snaps?

ubuntu linux snap cleanup bash

This knowledge base entry is for those using snap package manager. After using it for a while, you might notice that snap keeps previous versions of installed software. It is made in a smart way so that it does not take much space, however it creates loop devices for each version. This makes output of df -h command pretty unreadable. The first solution I've came up with is to remove old revisions so that precious SSD space is being freed and there are lessen loop devices.

Cleaning up Snaps

Surprisingly the snap itself  does not offer any tools for cleaning up old revisions. After googling and reading, I've came with solution for removing old revisions with this simple bash script (sorry credits are lost, I'm not the author of this script):

#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu

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

I've named this script snaps-clean and saved in by ~/bin/ directory. This script requires root to work properly, when ran without sudo will display appropriate message.

Results

Before applying script I had 36 loop devices! But you might have even more:

[~]$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            3,9G     0  3,9G   0% /dev
tmpfs           787M  2,1M  785M   1% /run
/dev/sda3       140G  109G   25G  82% /
tmpfs           3,9G   58M  3,8G   2% /dev/shm
tmpfs           5,0M  4,0K  5,0M   1% /run/lock
tmpfs           3,9G     0  3,9G   0% /sys/fs/cgroup
/dev/loop2      148M  148M     0 100% /snap/obs-studio/525
/dev/loop1      194M  194M     0 100% /snap/mailspring/368
/dev/loop3      2,3M  2,3M     0 100% /snap/gnome-calculator/260
/dev/loop4      1,0M  1,0M     0 100% /snap/gnome-logs/57
/dev/loop5      141M  141M     0 100% /snap/gnome-3-26-1604/88
/dev/loop0      152M  152M     0 100% /snap/arduino-mhall119/5
/dev/loop6       89M   89M     0 100% /snap/core/6964
/dev/loop10      89M   89M     0 100% /snap/core/7270
/dev/loop7      152M  152M     0 100% /snap/gnome-3-28-1804/59
/dev/loop9       94M   94M     0 100% /snap/telegram-desktop/753
/dev/loop13     4,2M  4,2M     0 100% /snap/gnome-calculator/406
/dev/loop14     3,8M  3,8M     0 100% /snap/gnome-system-monitor/91
/dev/loop12     108M  108M     0 100% /snap/robo3t-snap/3
/dev/loop15     111M  111M     0 100% /snap/robo3t-snap/4
/dev/loop18      35M   35M     0 100% /snap/gtk-common-themes/1122
/dev/loop17     148M  148M     0 100% /snap/obs-studio/439
/dev/loop11      36M   36M     0 100% /snap/gtk-common-themes/1198
/dev/loop19     4,2M  4,2M     0 100% /snap/gnome-calculator/352
/dev/loop22      54M   54M     0 100% /snap/core18/1013
/dev/loop25     225M  225M     0 100% /snap/polarr/9
/dev/loop23      35M   35M     0 100% /snap/gtk-common-themes/818
/dev/loop26     1,0M  1,0M     0 100% /snap/gnome-logs/61
/dev/loop28     265M  265M     0 100% /snap/phpstorm/98
/dev/loop30      15M   15M     0 100% /snap/gnome-logs/45
/dev/loop31      15M   15M     0 100% /snap/gnome-characters/288
/dev/loop32      95M   95M     0 100% /snap/telegram-desktop/818
tmpfs           787M  4,1M  783M   1% /run/user/1000
/dev/sda1        73G   55G   18G  76% /media/peter/Windows
/dev/loop34     194M  194M     0 100% /snap/mailspring/374
/dev/sdb1       932G  643G  290G  69% /media/peter/Dane
/dev/loop16      55M   55M     0 100% /snap/core18/1049
/dev/loop20     265M  265M     0 100% /snap/phpstorm/104
/dev/loop21     3,8M  3,8M     0 100% /snap/gnome-system-monitor/95
/dev/loop27      15M   15M     0 100% /snap/gnome-characters/292
/dev/loop35     141M  141M     0 100% /snap/gnome-3-26-1604/90
/dev/loop33     150M  150M     0 100% /snap/gnome-3-28-1804/63
/dev/sdc1       1,8T  1,1T  674G  62% /media/peter/Data2

The script runs pretty fast, and the results are sufficient, however we'll do another trick later. 

The output of the snaps-clean script
[~]$ sudo ./bin/snaps-clean 
core (revision 6964) removed
core18 (revision 1013) removed
gnome-3-26-1604 (revision 88) removed
gnome-3-28-1804 (revision 59) removed
gnome-calculator (revision 352) removed
gnome-calculator (revision 260) removed
gnome-characters (revision 288) removed
gnome-logs (revision 45) removed
gnome-logs (revision 57) removed
gnome-system-monitor (revision 91) removed
gtk-common-themes (revision 818) removed
gtk-common-themes (revision 1122) removed
mailspring (revision 368) removed
obs-studio (revision 439) removed
phpstorm (revision 98) removed
robo3t-snap (revision 3) removed
telegram-desktop (revision 753) removed

After cleaning up old snaps revisions, there are far less loop devices now:

[~]$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            3,9G     0  3,9G   0% /dev
tmpfs           787M  2,1M  785M   1% /run
/dev/sda3       140G  108G   26G  82% /
tmpfs           3,9G   87M  3,8G   3% /dev/shm
tmpfs           5,0M  4,0K  5,0M   1% /run/lock
tmpfs           3,9G     0  3,9G   0% /sys/fs/cgroup
/dev/loop2      148M  148M     0 100% /snap/obs-studio/525
/dev/loop0      152M  152M     0 100% /snap/arduino-mhall119/5
/dev/loop10      89M   89M     0 100% /snap/core/7270
/dev/loop13     4,2M  4,2M     0 100% /snap/gnome-calculator/406
/dev/loop15     111M  111M     0 100% /snap/robo3t-snap/4
/dev/loop11      36M   36M     0 100% /snap/gtk-common-themes/1198
/dev/loop25     225M  225M     0 100% /snap/polarr/9
/dev/loop26     1,0M  1,0M     0 100% /snap/gnome-logs/61
/dev/loop32      95M   95M     0 100% /snap/telegram-desktop/818
tmpfs           787M  4,1M  783M   1% /run/user/1000
/dev/sda1        73G   55G   18G  76% /media/peter/Windows
/dev/loop34     194M  194M     0 100% /snap/mailspring/374
/dev/sdb1       932G  643G  290G  69% /media/peter/Dane
/dev/loop16      55M   55M     0 100% /snap/core18/1049
/dev/loop20     265M  265M     0 100% /snap/phpstorm/104
/dev/loop21     3,8M  3,8M     0 100% /snap/gnome-system-monitor/95
/dev/loop27      15M   15M     0 100% /snap/gnome-characters/292
/dev/loop35     141M  141M     0 100% /snap/gnome-3-26-1604/90
/dev/loop33     150M  150M     0 100% /snap/gnome-3-28-1804/63
/dev/sdc1       1,8T  1,1T  659G  63% /media/peter/Data2

But for day to day usage it is still not like in the old days when df command were simply showing disks usage... I've found solution on Clinton's website (not sure if he was president), that basically creates alias for df command. The alias will hide all loop/tmp devices keeping only what's important. To get the whole list of block devices including loop devices and tmpfs use df with sudo.

Creating Alias for df

The alias will exclude squashfs and tmpfs file systems from df output. To exclude file system by type, the -x parameter can be used. Depending on your linux distribution, you might have .bash_aliases file or .bashrc in your home directory. It is preferred to add aliases, well to .bash_aliases:

echo "alias df='df -h -x squashfs -x tmpfs -x devtmpfs'" >> ~/.bash_aliases

After adding alias it is not available until bash is restarted. You can simply call bash after adding alias to make it active. The output of aliased command:

[~]$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3       140G  108G   26G  82% /
/dev/sda1        73G   55G   18G  76% /media/peter/Windows
/dev/sdb1       932G  643G  290G  69% /media/peter/Dane
/dev/sdc1       1,8T  1,2T  568G  68% /media/peter/Data2