clean snap packages in ubuntu

How to Clean Up Snap Packages in Linux

There are various ways to install and manage packages in Ubuntu/Debian systems, snap packages are one of them. Although snap packages take up more disk space than other package formats, some software are available only as snap, so they cannot be avoided altogether. But if you are not using certain snap packages on your system, they may end up taking unnecessary space. In this article, we will learn how to remove uninstalled snap packages and clean up snap packages in Linux.

How to Clean Up Snap Packages in Linux

Here are the steps to clean up snap packages in Linux.

1. Find Disk Space Occupied by Snap Packages

The system files about snap packages are stored at /var/lib/snapd. You can get an estimate of it using the following command.

$ sudo du -sh /var/lib/snapd
4.4G	/var/lib/snapd

As you can see it occupies a lot of space, mainly because snap keeps all the older versions of packages unless you configure it otherwise. Here is the command to list all snap packages.

$ snap list --all

In the output of above command, you will see the last column labeled Notes which displays the status of each package. If it says disabled, then it is not being used and you can safely delete it to free up disk space. When snap installs a newer version of a package or upgrades it to a newer version, it labels the older versions as disabled. This is how many unused snap packages accumulate in a system over time, and take up disk space.

2. Remove Unused Snap Packages

You can easily remove unused and disabled snap packages using a shell script. Create a blank shell script.

$ vi

Add the following code to it.

# Removes old revisions of snaps
set -eu
snap list --all | awk '/disabled/{print $1, $3}' |
    while read snapname revision; do
        snap remove "$snapname" --revision="$revision"

Save and close the file. Let us look at our shell script in detail. We basically run the ‘snap list –all’ command to get a list of all snap packages along with their status. We pass this output to awk command to search for lines where last column says disabled. We extract the 1st (snap name) and 3rd (revision number) column of all matching lines. We loop through each of these and call ‘snap remove’ command to delete the snap package.

Make the file executable.

$ chmod +x

You can run the file using the following command.

$ sudo bash

Once the script is run, you can check the space occupied by it once again, using the following command.

$ sudo du -sh /var/lib/snapd
2.9G	/var/lib/snapd

You can also create a cronjob to automatically run the script at regular intervals. Open crontab with the following command.

$ crontab -e

Add the following command to it. Please make sure to use the full path to your shell script so that it runs properly. The following command will run your shell script every day at 10.a.m. You can customize it as per your requirement using a crontab generator.

0 10 * * * sudo bash /home/ubuntu/  >/dev/null 2>&1

In this article, we have learnt how to clean up snap packages in Linux.

Also read:

How to Fix Unacceptable TLS Certificate Error in Linux
How to Reconfigure Installed Package in Ubuntu/Debian
Return False vs PreventDefault in JavaScript
How to Get Unique Values from JS Array
How to Disconnect User from SSH Connection

Leave a Reply

Your email address will not be published. Required fields are marked *