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 

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.