How to delete old snapshot versions to free up disk space

I recently used Disk Usage Analyzer to see if I could free up some space on my Ubuntu 18.10 desktop. /var/lib/snapd/snaps/ The folder is large.

While researching how to free up / clear the snapshot cache from /var/lib/snapd/snaps/ The snapshot package I have installed has not been deleted from the folder, and I find that by default, the system will store 3 snapshot versions after the snapshot package is updated. This means that for each installed snap package with at least 2 updates, I have stored 3 revisions on the system, which takes up a lot of disk space.
There is a snapshot option (since snapshot version 2.34), called refresh.retainTo set the maximum number of snapshot revisions stored by the system after the next refresh, which can be set to a number between 2 and 20. You can change it from the default of 3 to 2 using:

sudo snap set system refresh.retain=2

Relevant, but for Flatpak packages: How to remove unused Flatpak runtimes to free up disk space But what if you want to remove all versions remaining on the system for all snapshot packages with updates? this is A script Created by Canonical’s Ubuntu Engineering Community Manager Popey, it is used to delete all old version snapshots and keep only the currently active version (updated to LANG=en_US.UTF-8 So it works for non-English locales, thanks to William’s comment):

# Removes old revisions of snaps
set -eu

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

Using this script should free up some available disk space (depending on the number of snapshot packages installed on the system and whether they have updates since installation). In my case, the script was reduced /var/lib/snapd/snaps/ Clip more than 50%.
Update: The script seems to work only in English. For other languages ​​you need to replace /disabled/ In the command and its translation into your language.
To use this script, create a file named remove-old-snaps, Paste the contents of the above code block, save the file in your home directory, and make it executable with the following command:

chmod +x remove-old-snaps

Use run script sudo Delete the old snapshot revision (make sure to close all running snapshots before running the script):

sudo ./remove-old-snaps

This is a script that runs on my system, removing the old management pack revision:

$ sudo ./remove-old-snaps

atom (revision 223) removed
atom (revision 222) removed
bitwarden (revision 15) removed
bitwarden (revision 16) removed
canonical-livepatch (revision 50) removed
canonical-livepatch (revision 54) removed
chromium (revision 607) removed
chromium (revision 660) removed
core (revision 6531) removed
core (revision 6405) removed
core18 (revision 719) removed
core18 (revision 731) removed
gallery-dl (revision 36) removed
gallery-dl (revision 167) removed
gimp (revision 110) removed
gimp (revision 113) removed