5

Is there an equivalent snap command that does for Snaps what apt-get autoremove / apt-get autopurge does for Apt packages? (Also available as apt autoremove...)

Update 3: For the curious and those who end up here from Google, the equivalent command for FlatPak is flatpak uninstall --unused.

Update: I wanted to clarify the question after receiving an answer which states:

Snaps do not have dependencies like apt, so an equivalent to autoremove seems unnecessary.

This is incorrect. Consider the following (just one example of many):

$ snap list 
Name               Version          Rev    Tracking         Publisher      Notes
bare               1.0              5      latest/stable    canonical✓     base
core               16-2.57.6        14399  latest/stable    canonical✓     core
core18             20221212         2667   latest/stable    canonical✓     base
core20             20221123         1738   latest/stable    canonical✓     base
$ sudo snap install nvim --classic 
nvim v0.8.1 from neovim-snap (neovim-snap) installed
$ snap list 
Name               Version          Rev    Tracking         Publisher      Notes
bare               1.0              5      latest/stable    canonical✓     base
core               16-2.57.6        14399  latest/stable    canonical✓     core
core18             20221212         2667   latest/stable    canonical✓     base
core20             20221123         1738   latest/stable    canonical✓     base
core22             20221129         444    latest/stable    canonical✓     base
nvim               v0.8.1           2775   latest/stable    neovim-snap    classic
$ sudo snap remove nvim 
nvim removed
$ snap list 
Name               Version          Rev    Tracking         Publisher      Notes
bare               1.0              5      latest/stable    canonical✓     base
core               16-2.57.6        14399  latest/stable    canonical✓     core
core18             20221212         2667   latest/stable    canonical✓     base
core20             20221123         1738   latest/stable    canonical✓     base
core22             20221129         444    latest/stable    canonical✓     base
$

Before installing nvim, core22 was not installed. After removing it, core22 is left behind even though nothing depends on it now.

Update 2: In the above example, the installed dependency core22 is a base snap. Here's another example which demonstrates that the same issue is also present for non-base dependencies:

$ sudo snap list 
Name               Version          Rev    Tracking         Publisher      Notes
bare               1.0              5      latest/stable    canonical✓     base
core               16-2.57.6        14399  latest/stable    canonical✓     core
core18             20221212         2667   latest/stable    canonical✓     base
core20             20221123         1738   latest/stable    canonical✓     base
core22             20221129         444    latest/stable    canonical✓     base
$ sudo snap install falkon 
falkon 22.08.2 from KDE✓ installed
$ snap list 
Name                                  Version          Rev    Tracking         Publisher      Notes
bare                                  1.0              5      latest/stable    canonical✓     base
core                                  16-2.57.6        14399  latest/stable    canonical✓     core
core18                                20221212         2667   latest/stable    canonical✓     base
core20                                20221123         1738   latest/stable    canonical✓     base
core22                                20221129         444    latest/stable    canonical✓     base
falkon                                22.08.2          86     latest/stable    kde✓           -
kde-frameworks-5-98-qt-5-15-6-core20  5.98.0           9      latest/stable    kde✓           -
$ sudo snap remove falkon 
falkon removed
$ snap list 
Name                                  Version          Rev    Tracking         Publisher      Notes
bare                                  1.0              5      latest/stable    canonical✓     base
core                                  16-2.57.6        14399  latest/stable    canonical✓     core
core18                                20221212         2667   latest/stable    canonical✓     base
core20                                20221123         1738   latest/stable    canonical✓     base
core22                                20221129         444    latest/stable    canonical✓     base
kde-frameworks-5-98-qt-5-15-6-core20  5.98.0           9      latest/stable    kde✓           -
$ 

After installing and then removing the falkon snap, the unused dependency kde-frameworks-5-... is left behind.

P Varga
  • 328
  • 1
    @Terrance in the sense that nvim needs it, it's a "dependency". Yes it's also a base snap. And I am looking for an apt auroremove analog exactly because it did not remove when I removed nvim. – P Varga Dec 21 '22 at 08:46
  • Snaps do have dependencies (sort of)...but not like apt. Do not ignore the entire phrase. If you wish to add an autoremove feature to snapd, you are welcome to contribute the code. It's open source. – user535733 Dec 21 '22 at 10:22
  • 1
    The answer to this question might be interesting for you. – mook765 Dec 21 '22 at 11:26
  • If a base is required by a snap to work it will install it automatically. So, in that sense they appear to be a "dependency". However, you cannot remove a base snap as long as any snap is installed that requires it. Base snaps appear to be more of a "dependency" like Ubuntu is a requirement for you to run applications. There is a version of Ubuntu that is a Core version that is built mainly to support snaps and not .deb packages like non-Core versions of Ubuntu. – Terrance Dec 21 '22 at 14:37
  • 1
    Auto-removal of base snaps if they are not needed is something that Canonical has not included into snaps. If you feel this should be added you are more than welcome to contribute or file a bug against snapd for a feature request. – Terrance Dec 21 '22 at 14:43
  • 1
    @Terrance my example with nvim and core22 showed a "base snap" being installed. Now I've added another example with falkon which shows the same happens for a non-base dependency – P Varga Dec 21 '22 at 18:18
  • The point that I was trying to make is that this is not something that is included by Canonical. If you feel that it should be please contribute to it. We really cannot help you here as we are just volunteers and not Canonical employees. We can only do the same thing you can do. If you want it then contribute to it. What you're requesting is something that is not included it in right now. It is not in the man pages so it does not exist. – Terrance Dec 21 '22 at 19:30
  • 1
    By the way, non-base dependencies can be removed but the snap may not show correctly when it is removed, but the app itself will still work. – Terrance Dec 21 '22 at 19:34
  • @Terrance Ok thank you, got it. I was expecting that maybe this functionality already exists as a different command (like how apt-mark for example is a separate command from apt-get). But looks like that's not the case – P Varga Dec 21 '22 at 21:13
  • 1
  • @ArchismanPanigrahi No because there is apparently no equivalent of apt-get autopurge for Snap. – P Varga Jan 24 '23 at 23:19

2 Answers2

1

Snap AutoCleanUp

A often discussed question ... with a bunch of half-answers.

Since snap has no package systems that just upgrades in place , snap pulls dependencies with the packages and also keeps a copy of the last n versions of snaps

( and you are right if you think that snap uses your local storage like a mirrored registry with the last n versions )

Current State of Snap Auto-Cleaning:

In fact there is a Feature proposal since 2020 ( ~3 Years ) , so maybe spread some votes there ..

Snap Clean-Up / Auto-Purge Ways:

The behaviour closest to "auto-pruning" you can get:

  • Engage the auto-clean script to get rid of old versions ( found here )

  • disable auto-snapshots , their manual says it all:

    sudo snap set system snapshots.automatic.retention=no will disable it

    Automatic snapshot retention time is configured with the snapshots.automatic.retention system option. The default value is 31 days, and the value needs to be greater than 24 hours

    ...

    Disabling automatic snapshots will not affect preexisting, automatically generated snapshots, but only those generated by subsequent snap removals.

  • clean up the snapshots, there is a very good example script

There is no "exact-same" apt(-get) way of saving more space with snap , but amongst the top-tips there are:

  • Lower the "old versions" snapd keeps: sudo snap set system refresh.retain=2

    The refresh.retain value can be a number between 2 and 20. The default is refresh.retain=3 on Ubuntu Core systems and refresh.retain=2 on classic Ubuntu systems, such as those running Ubuntu 18.04 LTS (Bionic Beaver) and Ubuntu 16.04 LTS (Xenial Xerus).

  • clear the cache: sudo find /var/lib/snapd/cache/ -exec rm -v {} \; # Remove cache

The "Dangerous" non-moving part: locking against updates with refresh --hold

Preamble: You might run into outdated software and resulting security issues , so please be careful and update regularly by reinstalling or unholding the lock.

Another question also included:

even limiting unattended installation to Canonical-supported software

well

There was currently no way found to do this, except "pinning"( snap term "refresh hold")

How-To: snapd pinning

  • since 2.58+, there is refresh lock holding ( apt pinning equivalent )
  • it might be issued for one or all packages , with and without timelimit
  • snap refresh --hold "should" lock all updates
  • a specific example is sudo snap refresh --hold=24h firefox

How-To: update snapd "pinned" software

  • Variant A to update (firefox example), either sudo snap remove --purge firefox;sudo snap install firefox;sudo snap refresh --hold

  • Variant B to update sudo snap refresh --unhold firefox;sudo snap refresh firefox;sudo snap refresh --hold

0

After looking for a while, it is clear that there is no equivalent command currently.

P Varga
  • 328