Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

Ubuntu Snap Package Storage Optimization: A Complete Guide

Published

LinuxLinux and Commands

Ubuntu’s Snap package system is designed for convenience and security, letting you install software with all dependencies bundled together. While this makes installing and managing applications easier especially for developers and new users it comes with a downside: Snap packages can eat up a lot of disk space over time.

If you’ve ever run df -h and wondered why your root partition is mysteriously full, or why /var/lib/snapd/snaps/ seems to contain dozens of giant files, you’re not alone. Snap’s versioning and auto-update features, while handy, often leave behind multiple copies of apps and unused data that can quickly bloat storage, especially on servers or machines with small SSDs.

This guide will help you understand, manage, and optimize Snap’s storage footprint so you can reclaim disk space without breaking your favorite apps or your system.

1. Why Snap Packages Use So Much Space

a) Bundled Dependencies

Every Snap package includes its own dependencies even ones already on your system. This redundancy can make each Snap much larger than its traditional .deb equivalent.

b) Automatic Version Retention

Snap keeps at least two (sometimes more) versions of each installed package by default. This allows you to easily roll back to a previous version, but it also means old, unused versions pile up.

c) Data and Cache Buildup

Applications installed via Snap store user data and cache separately from the snaps themselves, often under ~/snap/ and /var/snap/. These can grow over time.

2. Checking Snap Storage Usage

a) List All Installed Snaps

See what snaps you have installed:

snap list

This will show package names, versions, revisions, and publisher info.

b) Check Space Used by Snap Packages

Find out how much space snaps are using:

sudo du -sh /var/lib/snapd/snaps/

This folder holds the actual squashfs images for all snaps.

Check user data:

du -sh ~/snap/

This shows how much data each user has stored for their snaps.

c) Find Old Versions

To see all versions (including old, unused ones), use:

snap list --all

You’ll see multiple revisions for many apps, with “disabled” next to old, unused versions.

3. Reclaiming Space: Step-by-Step

a) Remove Old Snap Versions

By default, Snap retains the two most recent versions of every app. You only need the current one. To clean up:

snap list --all | awk '/disabled/{print $1, $2, $3}' | \
while read snapname version revision; do
    sudo snap remove "$snapname" --revision="$revision"
done

This removes all disabled (old) revisions. Tip: If you’re new to scripts, you can also do this manually by looking for “disabled” snaps and running:

sudo snap remove <snapname> --revision=<revision>

b) Enable Automatic Old Revision Removal

As of Ubuntu 22.04+, Snap can auto-remove old revisions, but you can control how many are kept. Edit or create /etc/snapd/snapd.conf and add:

retain: 2

Then restart the Snap daemon:

sudo systemctl restart snapd

This ensures only two revisions are kept. Note: The default is already 2, but you can set it lower or higher if your use case demands.

c) Remove Unused Snaps

List all snaps and remove those you don’t use:

sudo snap remove <snapname>

Be especially ruthless with apps you tried once but don’t need.

d) Purge Orphaned Data

Even after removing snaps, user data may remain in ~/snap/<snapname>/. Delete it if you’re sure you don’t need it:

rm -rf ~/snap/<snapname>/

Do the same for system-level data in /var/snap/<snapname>/ if appropriate.

e) Clean Snap Cache

Snap sometimes caches downloads. Remove leftover .snap download files:

sudo rm -rf /var/lib/snapd/cache/*

4. Advanced Snap Storage Tuning

a) Change Snap Location (Advanced)

If you have a small root partition but a large secondary disk, you can move Snap’s storage directory. This is not officially supported and may break future updates, but some users symlink /var/lib/snapd to another location:

sudo systemctl stop snapd
sudo mv /var/lib/snapd /mnt/bigdisk/snapd
sudo ln -s /mnt/bigdisk/snapd /var/lib/snapd
sudo systemctl start snapd

Warning: This is a workaround. Always have backups before experimenting.

b) Schedule Regular Cleanup

Use a cron job or systemd timer to regularly remove old revisions:

Create a script, e.g., /usr/local/bin/snap-cleanup.sh:

#!/bin/bash
snap list --all | awk '/disabled/{print $1, $3}' | while read name rev; do
    snap remove "$name" --revision="$rev"
done

Make it executable:

sudo chmod +x /usr/local/bin/snap-cleanup.sh

Then add to root’s crontab (run sudo crontab -e):

0 2 * * * /usr/local/bin/snap-cleanup.sh

Now, cleanup runs every night at 2am.

5. Snap Alternatives and When to Avoid Snap

a) Prefer APT or Flatpak (When Possible)

If you’re on a server and don’t need Snap’s sandboxing, check if a .deb package is available:

sudo apt install <package>

Or use Flatpak for certain desktop apps if you prefer its storage behavior.

b) Remove Snapd Completely (For Minimalist Servers)

On some headless servers, Snap is unnecessary. You can remove Snap and its cache entirely:

sudo apt purge snapd
sudo rm -rf /var/lib/snapd
sudo rm -rf ~/snap

Warning: Some newer Ubuntu desktop features (like Software Updater, Chromium, etc.) now depend on Snap. Only do this on servers or minimal installations.

6. Monitoring Snap Storage Usage

a) Regularly Audit Snap Usage

Set a reminder to check Snap storage monthly using:

snap list --all
sudo du -sh /var/lib/snapd/snaps/

b) Watch Out for Zombie Data

After major upgrades or Snap removals, check for “ghost” data in ~/snap/ and /var/snap/.

7. Real-World Example: Snap Bloat on a Production Server

A sysadmin notices / is at 94% usage. Running df -h and du -sh /var/lib/snapd/snaps/ reveals Snap images are using 6GB. Investigation shows four versions of core, three versions of lxd, and several desktop apps installed as Snaps.

By running the cleanup script above and uninstalling unused snaps, 3.5GB is instantly reclaimed. The admin then sets up a cron job to keep Snap tidy in the future and enables stricter log rotation.

8. Best Practices for Snap Storage Optimization

  • Regularly remove old revisions don’t rely on Snap’s defaults.
  • Audit installed snaps and remove unused apps.
  • Clean up orphaned data in user and system Snap directories.
  • Set limits in Snapd config if you want stricter retention.
  • Monitor storage monthly set reminders or automate with scripts.

Conclusion

Snap brings convenience and security to Ubuntu, but it can easily bloat your storage with redundant versions and unused data. By understanding how Snap stores its packages, regularly cleaning up old revisions, and removing unused snaps, you can keep your Ubuntu system lean and efficient. Whether you’re running a desktop, a web server, or a cloud VM, a little Snap maintenance goes a long way.

Remember: Storage problems start small and grow quietly. Make Snap cleanup part of your regular Ubuntu maintenance routine your disks (and your uptime) will thank you!

Related guides

Account services are unavailable until the production backend is configured.