How to remove disabled (unused) snap packages with a single line of command?

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

#!/bin/sh
set -eu

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.


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

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 rm  --force /var/lib/snapd/cache/*  # Remove cache

You may want to remove snap altogether and install Flatpak.