Snap command to remove old snap revisions?

Is there a snap command to remove previous revisions of the currently installed snaps?

Currently I have 3 revisions of every installed snap sitting on my PC and taking up much of my ssd space. Since every snap works as expected, I’d like to remove all the previous revisions with one snap command if possible.

Currently, this is not possible. Don’t forget that the /snap directory is larger than the size of the snap files on disk because the snaps are stored in a compressed file. You can see the actual used space of the snap files with:

du -sh /var/lib/snapd/snaps
2 Likes

snap directory is 1.1GB but running the command you gave me shows that it’s really 14GB :open_mouth:

I hope a command to clear the cached snaps gets implemented because I don’t have any reason to have 2/3 of the snaps stored on my PC if every snap is working correctly.

#!/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"

e.g.

$ df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p3  462G  147G  293G  34% /

Ran the above… 2m42s later…

$ df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p3  462G  133G  306G  31% /
2 Likes

Yeah, that should work great until hopefully there is a tool like that integrated into snap itself. Thanks @popey :smiley:

And just in case someone like me stumble upon this 3 years later and wonders why it’s not working : your system might not be in english ! So you’ll need to force snap list to speak english :

#! /usr/bin/env bash

# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS

set -eu

if [ "${UID}" -ne 0 ]; then
    echo "This script should be run as root. Try 'sudo'."
    exit 1
fi

LANG=en_US.UTF-8 snap list --all | awk '/disabled/{print $1, $3}' |
    while read -r snapname revision; do
        snap remove "${snapname}" --revision="${revision}"
    done
3 Likes