DEV Community


Posted on

How to Clean Up Snap Versions to Free Up Disk Space

(see: My source of information)

Symptom: the partition containing /var is running out of diskspace.

OS: Linux Ubuntu

This quick guide with a script helps to clean up old snap versions and free some disk space in your Ubuntu systems.

Snap can consume a considerable amount of storage space because it keeps old revisions of a package for retention.

The default value is 3 for several revisions for retention. That means Snap keeps three older versions of each package, including the active version. This is okay if you do not have constraints on your disk space.

But for servers and other use cases, this can easily run into cost issues, consuming your disk space.

However, you can easily modify the count using the following command. The value can be between 2 to 20.

sudo snap set system refresh.retain=2
Enter fullscreen mode Exit fullscreen mode

Clean Up Snap Versions

In a post in SuperUser, Popey, the ex-Engineering Manager at Canonical, provided a simple script that can clean up old versions of Snaps and keep the latest one.

Fire-up nano to create a new script in /bin:

sudo nano /bin/
Enter fullscreen mode Exit fullscreen mode

Here’s the script we will use to clean the Snap up.

 #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"
Enter fullscreen mode Exit fullscreen mode

Save the file by pressing ctrl+x, y in nano.

Make it executable:

sudo chmod +x /bin/
Enter fullscreen mode Exit fullscreen mode

CLOSE ALL SNAPS and then run the script to clean old snaps:

sudo /bin/
Enter fullscreen mode Exit fullscreen mode

Top comments (0)