Software & AppsOperating SystemLinux

How To Clear Snap Cache and Remove Uninstalled Snaps on Ubuntu

Ubuntu 14

In this article, we will guide you through the process of clearing your Snap cache and removing uninstalled Snaps on Ubuntu. This is an essential task for maintaining a clean and efficient system, especially if you frequently install and uninstall Snap packages.

Quick Answer

To clear Snap cache and remove uninstalled Snaps on Ubuntu, open a Terminal and run the command sudo sh -c 'rm -rf /var/lib/snapd/cache/*'. This will remove all files in the cache directory. Alternatively, you can use the command sudo find /var/lib/snapd/cache/ -exec rm -v {} \; to remove the files one by one. Remember that removing the cache files will result in a fresh download when you reinstall a snap.

Understanding Snap and Snap Cache

Snap is a software packaging and deployment system developed by Canonical for operating systems that use the Linux kernel. The packages, called ‘snaps’, and the tool for using them, ‘snapd’, work across a range of Linux distributions allowing distribution-agnostic upstream software packaging.

When you install a Snap, Snapd downloads the package and stores a copy in its cache. This is beneficial because if you need to reinstall the Snap, Snapd can use the cached copy instead of downloading it again. However, if you uninstall a Snap and don’t plan on reinstalling it, the cached copy is just wasting disk space. That’s why it’s a good idea to clear your Snap cache regularly.

Clearing Snap Cache

To clear your Snap cache, follow the steps below:

  1. Open a Terminal

You can open a terminal by pressing Ctrl + Alt + T or searching for ‘Terminal’ in your system’s application launcher.

  1. Run the Cache Clearing Command

Enter the following command to remove all files in the cache directory:

sudo sh -c 'rm -rf /var/lib/snapd/cache/*'

This command uses rm, which is a standard command for removing files and directories in Unix-like operating systems. The -rf option tells rm to remove files and directories recursively (-r) and without asking for confirmation (-f). /var/lib/snapd/cache/* is the path to the cache directory of snapd.

  1. Alternative Command

Alternatively, you can use the following command to remove the files one by one:

sudo find /var/lib/snapd/cache/ -exec rm -v {} \;

This command uses find to search for files in the specified directory (/var/lib/snapd/cache/). The -exec option allows you to execute a command on each found file. The command rm -v {} \; removes each file and uses the -v (verbose) option to print a message for each removed file.

  1. Enter Sudo Mode

If you encounter any issues with the above commands, try entering sudo mode completely with sudo -i before executing the commands.

Please note that removing the cache files will result in a fresh download when you reinstall a snap.

Removing Old Snap Versions

Snapd keeps up to 20 older versions of a snap by default. If you want to reclaim more storage, you can remove these older versions using the following script:

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

This script will remove the disabled snaps and their corresponding revisions. The snap list --all command lists all snaps, the awk command filters out the disabled ones, and the snap remove command removes each disabled snap.

Conclusion

In conclusion, clearing your Snap cache and removing old Snap versions can help you reclaim disk space and keep your system clean. However, please remember that these operations may result in the loss of user settings and dependencies. Always make sure to back up any important data before performing these tasks. If you’re migrating from a snap package to a native package, using sudo snap remove --purge $PACKAGE may be more suitable. However, this command may not work for uninstalled packages. For more information about Snap, please refer to the official Snap documentation.

Why is it important to clear the Snap cache?

Clearing the Snap cache is important to reclaim disk space on your system. When you uninstall a Snap, the cached copy is not automatically removed, which can result in wasted disk space over time. Clearing the cache ensures that you are only storing necessary files on your system.

How often should I clear the Snap cache?

The frequency of clearing the Snap cache depends on your usage. If you frequently install and uninstall Snap packages, it is recommended to clear the cache regularly to prevent unnecessary disk space usage. You can schedule it monthly or whenever you feel that your system is running low on disk space.

Will clearing the Snap cache affect my installed Snaps?

Clearing the Snap cache will not affect your installed Snaps. It only removes the cached copies of uninstalled Snaps. Your installed Snaps will continue to function normally.

Can I recover the cleared Snap cache files?

No, once you clear the Snap cache, the files are permanently deleted. There is no built-in way to recover them. It is always recommended to make a backup of any important files before performing any operations that involve deleting data.

What happens if I don’t clear my Snap cache?

If you don’t clear your Snap cache, it will continue to accumulate cached copies of uninstalled Snaps. This can lead to unnecessary disk space usage over time. Clearing the cache helps optimize your system’s storage and maintain a clean environment.

Can I remove specific Snap cache files instead of clearing the entire cache?

Yes, you can remove specific Snap cache files instead of clearing the entire cache. Simply navigate to the cache directory (/var/lib/snapd/cache/) and delete the specific files you want to remove. However, it is generally easier and more efficient to clear the entire cache using the provided commands.

Is it safe to remove old Snap versions?

Yes, it is safe to remove old Snap versions. Snapd keeps up to 20 older versions of a Snap by default, and removing them can help free up storage space on your system. However, please note that removing old versions may result in the loss of user settings and dependencies associated with those versions. Always make sure to back up any important data before removing old Snap versions.

Leave a Comment

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