How to Clean Up Snap Versions to Free Up Disk Space

3 min


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

I was running out of disk space in my test system with Ubuntu. So I was investigating via GNOME’s Disk Usage Analyser to find out which package is consuming the precious SSD space. Apart from the usual cache, home directory – to my surprise, I found that Snap and Flatpak consuming a considerable amount of storage space.

Snap size - before cleanup
Snap size – before cleanup

Although, I always maintain a rule – not to use Snap or Flatpak unless absolutely necessary. This is mainly because of their installation size and other issues. I prefer vanilla deb, rpm packages. Over the years, I have installed and removed a certain amount of Snap packages in this test system. Despite snap is uninstalled properly, it kept some files as well.

So I opened the Snap folder under /var/lib/snapd/snaps and found out that Snap is keeping track of older versions of previously installed/uninstalled packages.

For example, in the below image, you can see GNOME 3.28, 3.34, Wine – all of these are removed long back. But they are still there. This is by the Snap design. So, by design Snap keeps versions of uninstalled packages as well.

Files under snaps directory
Files under snaps directory

Alternatively, you can get the same in terminal using:

snap list --all
snap list all
snap list all

The default value is 3 for several revisions for retention. That means Snap keeps 3 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, 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

Clean Up Snap Versions

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

Here’s the script which we are going to use to clean the Snap up.

#!/bin/bash
 #Removes old revisions of snaps
 #CLOSE ALL SNAPS BEFORE RUNNING THIS
 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"
     done

Save the above script as .sh in a directory (for example clean_snap.sh), give it executable permission and run.

chmod +x clean_snap.sh

When I ran the script, it reduced a lot of disk space. The script would also show the name of the package being removed.

Executing the script
Executing the script
Snaps size after cleanup
Snaps size after cleanup

Closing Notes

There are always debates on how efficient Snap design is. Many say it is broken by design, bloated, heavy on systems, and whatnot. Some part of that argument is true, I would not deny. The whole concept of sandboxing application is great if implemented and enhanced properly. I believe, Flatpak does a better job compared to Snap.

That said, I hope this helps you clean up some disk space. Although it is tested in Ubuntu, should work in all Linux distribution that supports Snap. Also, check out our guide on how to clean up Ubuntu with additional steps.


We bring the latest tech, software news and stuff that matters. Stay in touch via Telegram, Twitter, YouTube, and Facebook and never miss an update!

Join our Telegram channel and stay informed on the move.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.


Arindam

Creator of debugpoint.com. All time Linux user and open-source supporter. Connect with me via Telegram, Twitter, LinkedIn, or send us an email.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

6 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
6
0
Would love your thoughts, please comment.x
()
x