Categories
DailyOps

How to remove disabled snaps

Remove disabled snaps which are not longer used.

List installed snaps. Notice, some of these are disabled as there are multiple revisions available.

$ snap list --all
Name               Version                     Rev    Tracking         Publisher     Notes
bare               1.0                         5      latest/stable    canonical*    base
chromium           98.0.4758.102               1912   latest/stable    canonical*    -
chromium           98.0.4758.80                1899   latest/stable    canonical*    disabled
code               f80445ac                    88     latest/stable    vscode*       classic
code               d6ee99e4                    87     latest/stable    vscode*       disabled,classic
core               16-2.54.3                   12725  latest/stable    canonical*    core
core18             20211028                    2253   latest/stable    canonical*    base,disabled
core18             20211215                    2284   latest/stable    canonical*    base
core20             20220215                    1361   latest/stable    canonical*    base
core20             20220114                    1328   latest/stable    canonical*    base,disabled
firefox            97.0.1-1                    996    latest/stable/…  mozilla*      disabled
firefox            97.0.1-1                    1025   latest/stable/…  mozilla*      -
gimp               2.10.28                     383    latest/stable    snapcrafters  -
gnome-3-28-1804    3.28.0-19-g98f9e67.98f9e67  161    latest/stable    canonical*    -
gnome-3-38-2004    0+git.1f9014a               99     latest/stable/…  canonical*    -
gnome-3-38-2004    0+git.cd626d1               87     latest/stable/…  canonical*    disabled
gtk-common-themes  0.1-59-g7bca6ae             1519   latest/stable/…  canonical*    -
obs-studio         27.1.3                      1284   latest/stable    snapcrafters  -
rpi-imager         1.7.1                       221    latest/stable    popey         -
snap-store         3.38.0-66-gbd5b8f7          558    latest/stable/…  canonical*    -
snap-store         3.38.0-66-gbd5b8f7          557    latest/stable/…  canonical*    disabled

These disabled snaps take up unnecessary space.

$ du -hs  /snap/snap-store/557
165M	/snap/snap-store/557

You can safely remove older revisions, but doing this by hand can get tedious quickly.

$ sudo snap remove snap-store --revision 557
snap-store (revision 557) removed

Create a simple shell script.

#!/bin/bash
# snapcleanup - remove disabled snaps

# text formatting
text_bold=$(tput bold)
text_normal=$(tput sgr0)

if [ "$#" -eq "1" ] && ( [ "$1" == "--dry-run" ] || [ "$1" == "--execute" ]); then
  all_snaps="$(snap list --all)"
  disabled_snaps="$(echo -e "${all_snaps}" | awk '$6~/disabled/ {print $1, $3}')"

  if [ -n "$disabled_snaps" ]; then
    echo "Snaps to be removed: "
    while IFS=\n read -r line; do
        snap_name="$(echo $line | awk '{print $1}')"
        snap_revision="$(echo $line | awk '{print $2}')"
        snap_size="$(du --human-readable --summarize /snap/${snap_name}/${snap_revision} 2>/dev/null | awk '{print $1}')"

        if [ "$1" == "--dry-run" ]; then
          echo " - ${text_bold}${snap_name}${text_normal} revision ${text_bold}${snap_revision}${text_normal} size ${text_bold}${snap_size}${text_normal}"
        elif [ "$1" == "--execute" ]; then
          sudo snap remove ${snap_name} --revision ${snap_revision}
        fi
    done <<< "$disabled_snaps"
  fi
else
  echo "snapcleanup"  
  echo ""
  echo "Parameters:"
  echo "  --dry-run"
  echo "    display snaps to be removed"
  echo "  --execute"
  echo "    remove disabled snaps"
fi

Display help information.

$ bash snapcleanup 
snapcleanup

Parameters:
  --dry-run
    display snaps to be removed
  --execute
    remove disabled snaps

Perform dry-run to see what will be removed.

$ snapcleanup --dry-run
Snaps to be removed: 
 - chromium revision 1899 size 295M
 - code revision 87 size 667M
 - core18 revision 2253 size 169M
 - core20 revision 1328 size 198M
 - firefox revision 996 size 328M
 - gnome-3-38-2004 revision 87 size 1.1G

Remove disabled snaps to reclaim free space.

$ sudo snapcleanup --execute
Snaps to be removed: 
chromium (revision 1899) removed
code (revision 87) removed
core18 (revision 2253) removed
core20 (revision 1328) removed
firefox (revision 996) removed
gnome-3-38-2004 (revision 87) removed

Simple as that.