How to Clean Up Snap Package Versions in Linux [Quick Tip]

Snap packages are not everyone’s favorite but they are an integral part of the Ubuntu ecosystem.

It has its pros and cons. One of the negatives is that Snap packages are usually bigger in size and take a lot of disk space.

This could be a problem if you are running out of disk space, specially on the root partition.

Let me share a neat trick that you could use to cut down the disk spaced used by Snap packages.

Cleaning up old Snap package versions to free disk space

The system files related to snap are stored in the /var/lib/snapd directory. Based on the number of Snap packages you have installed, this directory size could be in several GBs.

Don’t just take my word for it. Do an assesement by using the du command to check the directory size.

abhishek@its-foss:~$ sudo du -sh /var/lib/snapd
5.4G	/var/lib/snapd

You may also use the Disk Usage Analyzer GUI tool to see the disk usage in Ubuntu.

snap disk usage
Snap disk usage

That’s a lot, right? You could free up some disk space here.

By design, Snap keeps at least one older version of the packages you have installed on your system.

You can see this behavior by using the Snap command:

snap list --all

You should see the same package listed twice with different version and revision number.

snap keeps two versions of each package
Snap keeps at least two versions of each package

To free up disk space, you can delete the additional package versions. How do you know which one to delete? You can see that these older packages are labeled ‘disabled’.

Don’t worry. You don’t have to manually do it. There is sort of automatic way to do it thanks to a nifty bash script written by Alan Pope while he was working in the Snapcraft team.

Kindly note that this script may not work for French and some other languages because the label ‘disabled’ is likely to be different in different languages.

I hope you know how to create and run a bash shell script. Basically, create a new file named clean-snap.sh and add the following lines to it.

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

Save it and close the editor.

To run this script, keep it in your home directory and then open the terminal in Ubuntu and run this command:

sudo bash clean-snap.sh

You can see that it starts removing the older version of packages.

removing old snap package versions
Removing old snap package versions

If you check the disk space used by Snap now, you’ll see that the directory size is reduced now.

abhishek@its-foss:~$ sudo du -sh /var/lib/snapd
3.9G	/var/lib/snapd

If this works for you, you could run this command occasionally.

How does this script work?

If you are curious about what does this script do, let me explain.

You have already seen the output of the “snap list –all” command. It’s output is passed to the awk command. Awk is a powerful scripting tool.

The awk ‘/disabled/{print $1, $3}’ part looks for the string ‘disabled’ in each row and if it is found, it extracts the first column and third column.

This output is further passed to a combination of while and read command. Read command gets the value of first column to snapname and third column to revision variable.

These variables are then used to run the snap remove command to delete with the name of the span package name and its revision number.

The while loop runs as long as there are rows found with ‘disabled’ string in it.

This all makes sense easily if you know a little bit about shell scripting. If you are not familiar with, we have a bash tutorial series for beginners for you.

Did you get your GBs back?

You may see some forums advising to set up the Snap package retention value to 2.

sudo snap set system refresh.retain=2

I don’t think it’s needed anymore. Snap’s default behavior now is to store total 2 versions for any package.

Altogether, if you are running out of space, getting rid of the additional package version could surely one of the ways to free up disk space on Ubuntu.

If this tutorial helped you free some space, let me know in the comment section.

About the author
Abhishek Prakash

Abhishek Prakash

Created It's FOSS 11 years ago to share my Linux adventures. Have a Master's degree in Engineering and years of IT industry experience. Huge fan of Agatha Christie detective mysteries 🕵️‍♂️

It's FOSS

Making You a Better Linux User

It's FOSS

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to It's FOSS.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.