Repository: flatpak/flatpak (C)

Issue:
[Bug]: multiple nvidia runtimes on every Flatpak app update, all listed as in use by `uninstall --unused` command
### Checklist

- [X] I agree to follow the [Code of Conduct](https://github.com/flatpak/flatpak/blob/main/CODE_OF_CONDUCT.md) that this project adheres to.
- [X] I have searched the [issue tracker](https://www.github.com/flatpak/flatpak/issues) for a bug that matches the one I want to file, without success.
- [X] If this is an issue with a particular app, I have tried filing it in the appropriate issue tracker for the app (e.g. under https://github.com/flathub/) and determined that it is an issue with Flatpak itself.
- [X] This issue is not a report of a security vulnerability (see [here](https://github.com/flatpak/flatpak/blob/main/SECURITY.md) if you need to report a security issue).

### Flatpak version

1.15.1 originally, issue still experienced while using 1.15.4

(do not perceive lack of further updates to this version number as a sign the issue has been resolved)

### What Linux distribution are you using?

Manjaro Linux

### Linux distribution version

6.0.15-1 originally, issue still experienced while using 6.4.3-1

(do not perceive lack of further updates to this version number as a sign the issue has been resolved)

### What architecture are you using?

x86_64

### How to reproduce

Any Flatpak app has an update available through Manjaro's default Add/Remove Software tool, then run `flatpak uninstall --unused` to clean up.

### Expected Behavior

Only the app and the necessary supporting files are updated, all redundant / unnecessary packages and runtimes are removed.

### Actual Behavior

Flatpak updates multiple redundant versions of `org.freedesktop.Platform.GL.nvidia-*` each over 300MB.

```
Upgrading runtime/org.freedesktop.Platform.GL.nvidia-495-44/x86_64/1.4
Downloading: 282.3 MB/282.3 MB (398.1 kB/s)
Upgrading runtime/org.freedesktop.Platform.GL.nvidia-510-47-03/x86_64/1.4
Downloading: 328.8 MB/328.8 MB (340.8 kB/s)
Upgrading runtime/org.freedesktop.Platform.GL.nvidia-510-54/x86_64/1.4
Downloading: 328.9 MB/328.9 MB (217.1 kB/s)
Upgrading runtime/org.freedesktop.Platform.GL.nvidia-510-60-02/x86_64/1.4
Downloading: 328.3 MB/328.3 MB (318.4 kB/s)
Upgrading runtime/org.freedesktop.Platform.GL.nvidia-510-68-02/x86_64/1.4
Downloading: 329.3 MB/329.2 MB (202.1 kB/s)
Downloading: 329.3 MB/329.2 MB (202.1 kB/s)
Upgrading runtime/org.freedesktop.Platform.GL.nvidia-510-73-05/x86_64/1.4
Downloading: 329.3 MB/329.3 MB (194.4 kB/s)
Upgrading runtime/org.freedesktop.Platform.GL.nvidia-515-48-07/x86_64/1.4
Downloading extra data: 360.5 MB/360.5 MB (206.8 kB/s)
Upgrading runtime/org.freedesktop.Platform.GL.nvidia-515-57/x86_64/1.4
Downloading extra data: 363.4 MB/363.4 MB (350.1 kB/s)
Upgrading runtime/org.freedesktop.Platform.GL.nvidia-515-65-01/x86_64/1.4
Downloading: 364.2 MB/364.2 MB (416.7 kB/s)
Upgrading runtime/org.freedesktop.Platform.GL.nvidia-520-56-06/x86_64/1.4
Downloading: 406.2 MB/406.2 MB (610.8 kB/s)
Upgrading runtime/org.freedesktop.Platform.GL.nvidia-525-60-11/x86_64/1.4
Downloading: 413.9 MB/413.9 MB (541.0 kB/s)
```

Attempting to clean up the system with `flatpak uninstall --unused` lists all these runtimes as being used by all installed Flatpak apps, prompting for confirmation to remove on each one - this implies the `--unused` flag is being ignored, or the output of the command is not written correctly.

```
flatpak uninstall --unused                                                                                                                                                           ✔ 
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-520-56-06 branch 1.4:
   com.github.marktext.marktext, com.github.Eloston.UngoogledChromium, org.libreoffice.LibreOffice, com.vscodium.codium, com.dropbox.Client, com.github.eneshecan.WhatsAppForLinux, com.github.johnfactotum.Foliate, com.usebottles.bottles, org.flameshot.Flameshot, ua.org.brezblock.q4wine
Really remove? [y/n]: yn
Really remove? [y/n]: n
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-515-65-01 branch 1.4:
   com.github.marktext.marktext, com.github.Eloston.UngoogledChromium, org.libreoffice.LibreOffice, com.vscodium.codium, com.dropbox.Client, com.github.eneshecan.WhatsAppForLinux, com.github.johnfactotum.Foliate, com.usebottles.bottles, org.flameshot.Flameshot, ua.org.brezblock.q4wine
Really remove? [y/n]: n
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-515-57 branch 1.4:
   com.github.marktext.marktext, com.github.Eloston.UngoogledChromium, org.libreoffice.LibreOffice, com.vscodium.codium, com.dropbox.Client, com.github.eneshecan.WhatsAppForLinux, com.github.johnfactotum.Foliate, com.usebottles.bottles, org.flameshot.Flameshot, ua.org.brezblock.q4wine
Really remove? [y/n]: n
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-515-48-07 branch 1.4:
   com.github.marktext.marktext, com.github.Eloston.UngoogledChromium, org.libreoffice.LibreOffice, com.vscodium.codium, com.dropbox.Client, com.github.eneshecan.WhatsAppForLinux, com.github.johnfactotum.Foliate, com.usebottles.bottles, org.flameshot.Flameshot, ua.org.brezblock.q4wine
Really remove? [y/n]: n
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-510-73-05 branch 1.4:
   com.github.marktext.marktext, com.github.Eloston.UngoogledChromium, org.libreoffice.LibreOffice, com.vscodium.codium, com.dropbox.Client, com.github.eneshecan.WhatsAppForLinux, com.github.johnfactotum.Foliate, com.usebottles.bottles, org.flameshot.Flameshot, ua.org.brezblock.q4wine
Really remove? [y/n]: n
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-510-68-02 branch 1.4:
   com.github.marktext.marktext, com.github.Eloston.UngoogledChromium, org.libreoffice.LibreOffice, com.vscodium.codium, com.dropbox.Client, com.github.eneshecan.WhatsAppForLinux, com.github.johnfactotum.Foliate, com.usebottles.bottles, org.flameshot.Flameshot, ua.org.brezblock.q4wine
Really remove? [y/n]: n
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-510-60-02 branch 1.4:
   com.github.marktext.marktext, com.github.Eloston.UngoogledChromium, org.libreoffice.LibreOffice, com.vscodium.codium, com.dropbox.Client, com.github.eneshecan.WhatsAppForLinux, com.github.johnfactotum.Foliate, com.usebottles.bottles, org.flameshot.Flameshot, ua.org.brezblock.q4wine
Really remove? [y/n]: n
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-510-54 branch 1.4:
   com.github.marktext.marktext, com.github.Eloston.UngoogledChromium, org.libreoffice.LibreOffice, com.vscodium.codium, com.dropbox.Client, com.github.eneshecan.WhatsAppForLinux, com.github.johnfactotum.Foliate, com.usebottles.bottles, org.flameshot.Flameshot, ua.org.brezblock.q4wine
Really remove? [y/n]: n
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-510-47-03 branch 1.4:
   com.github.marktext.marktext, com.github.Eloston.UngoogledChromium, org.libreoffice.LibreOffice, com.vscodium.codium, com.dropbox.Client, com.github.eneshecan.WhatsAppForLinux, com.github.johnfactotum.Foliate, com.usebottles.bottles, org.flameshot.Flameshot, ua.org.brezblock.q4wine
Really remove? [y/n]: n
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-495-44 branch 1.4:
   com.github.marktext.marktext, com.github.Eloston.UngoogledChromium, org.libreoffice.LibreOffice, com.vscodium.codium, com.dropbox.Client, com.github.eneshecan.WhatsAppForLinux, com.github.johnfactotum.Foliate, com.usebottles.bottles, org.flameshot.Flameshot, ua.org.brezblock.q4wine
Really remove? [y/n]: n


        ID                                                   Branch                  Op
 1.     com.github.PintaProject.Pinta.Locale                 stable                  r
 2.     org.freedesktop.Platform.Compat.i386                 21.08                   r
 3.     org.freedesktop.Platform.GL32.default                21.08                   r
 4.     org.freedesktop.Platform.ffmpeg-full                 21.08                   r
 5.     org.freedesktop.Platform.ffmpeg_full.i386            21.08                   r
 6.     org.gnome.Platform.Compat.i386                       42                      r
 7.     org.gnome.Platform                                   42                      r
 8.     org.gnome.Platform.Locale                            42                      r
 9.     org.winehq.Wine.DLLs.dxvk                            stable-21.08            r
10.     org.winehq.Wine.gecko                                stable-21.08            r
11.     org.winehq.Wine.mono                                 stable-21.08            r

Proceed with these changes to the system installation? [Y/n]: n
````

### Additional Information

Output from `flatpak list --runtime` shows most things have duplicates split between branches 21.08 and 22.08 along with all nvidia runtimes and their GL32 versions:

```
flatpak list --runtime                                                                                                                                ✔ 
Name                                           Application ID                                          Version           Branch                Installation
Codecs                                         com.github.Eloston.UngoogledChromium.Codecs                               stable                system
Freedesktop Platform                           org.freedesktop.Platform                                21.08.18          21.08                 system
Freedesktop Platform                           org.freedesktop.Platform                                22.08.7           22.08                 system
i386                                           org.freedesktop.Platform.Compat.i386                                      21.08                 system
i386                                           org.freedesktop.Platform.Compat.i386                                      22.08                 system
Mesa                                           org.freedesktop.Platform.GL.default                     21.3.9            21.08                 system
Mesa                                           org.freedesktop.Platform.GL.default                     22.3.4            22.08                 system
Mesa (Extra)                                   org.freedesktop.Platform.GL.default                     22.3.4            22.08-extra           system
nvidia-495-44                                  org.freedesktop.Platform.GL.nvidia-495-44                                 1.4                   system
nvidia-510-47-03                               org.freedesktop.Platform.GL.nvidia-510-47-03                              1.4                   system
nvidia-510-54                                  org.freedesktop.Platform.GL.nvidia-510-54                                 1.4                   system
nvidia-510-60-02                               org.freedesktop.Platform.GL.nvidia-510-60-02                              1.4                   system
nvidia-510-68-02                               org.freedesktop.Platform.GL.nvidia-510-68-02                              1.4                   system
nvidia-510-73-05                               org.freedesktop.Platform.GL.nvidia-510-73-05                              1.4                   system
nvidia-515-48-07                               org.freedesktop.Platform.GL.nvidia-515-48-07                              1.4                   system
nvidia-515-57                                  org.freedesktop.Platform.GL.nvidia-515-57                                 1.4                   system
nvidia-515-65-01                               org.freedesktop.Platform.GL.nvidia-515-65-01                              1.4                   system
nvidia-520-56-06                               org.freedesktop.Platform.GL.nvidia-520-56-06                              1.4                   system
nvidia-525-60-11                               org.freedesktop.Platform.GL.nvidia-525-60-11                              1.4                   system
nvidia-525-85-05                               org.freedesktop.Platform.GL.nvidia-525-85-05                              1.4                   system
nvidia-525-89-02                               org.freedesktop.Platform.GL.nvidia-525-89-02                              1.4                   system
default                                        org.freedesktop.Platform.GL32.default                                     21.08                 system
Mesa                                           org.freedesktop.Platform.GL32.default                   22.3.4            22.08                 system
nvidia-510-54                                  org.freedesktop.Platform.GL32.nvidia-510-54                               1.4                   system
nvidia-510-60-02                               org.freedesktop.Platform.GL32.nvidia-510-60-02                            1.4                   system
nvidia-510-68-02                               org.freedesktop.Platform.GL32.nvidia-510-68-02                            1.4                   system
nvidia-510-73-05                               org.freedesktop.Platform.GL32.nvidia-510-73-05                            1.4                   system
nvidia-515-48-07                               org.freedesktop.Platform.GL32.nvidia-515-48-07                            1.4                   system
nvidia-515-57                                  org.freedesktop.Platform.GL32.nvidia-515-57                               1.4                   system
nvidia-515-65-01                               org.freedesktop.Platform.GL32.nvidia-515-65-01                            1.4                   system
nvidia-520-56-06                               org.freedesktop.Platform.GL32.nvidia-520-56-06                            1.4                   system
nvidia-525-60-11                               org.freedesktop.Platform.GL32.nvidia-525-60-11                            1.4                   system
nvidia-525-85-05                               org.freedesktop.Platform.GL32.nvidia-525-85-05                            1.4                   system
nvidia-525-89-02                               org.freedesktop.Platform.GL32.nvidia-525-89-02                            1.4                   system
ffmpeg-full                                    org.freedesktop.Platform.ffmpeg-full                                      21.08                 system
ffmpeg-full                                    org.freedesktop.Platform.ffmpeg-full                                      22.08                 system
i386                                           org.freedesktop.Platform.ffmpeg_full.i386                                 21.08                 system
openh264                                       org.freedesktop.Platform.openh264                       2.1.0             2.0                   system
openh264                                       org.freedesktop.Platform.openh264                       2.1.0             2.2.0                 system
Freedesktop SDK                                org.freedesktop.Sdk                                     22.08.7           22.08                 system
GNOME Application Platform version 42          org.gnome.Platform                                                        42                    system
GNOME Application Platform version 43          org.gnome.Platform                                                        43                    system
i386                                           org.gnome.Platform.Compat.i386                                            42                    system
i386                                           org.gnome.Platform.Compat.i386                                            43                    system
Adwaita theme                                  org.kde.KStyle.Adwaita                                                    5.15-21.08            system
Adwaita theme                                  org.kde.KStyle.Adwaita                                                    5.15-22.08            system
KDE Application Platform                       org.kde.Platform                                                          5.15-21.08            system
KDE Application Platform                       org.kde.Platform                                                          5.15-22.08            system
DXVK                                           org.winehq.Wine.DLLs.dxvk                               1.10.3            stable-21.08          system
DXVK                                           org.winehq.Wine.DLLs.dxvk                               1.10.3            stable-22.08          system
Gecko                                          org.winehq.Wine.gecko                                                     stable-21.08          system
Gecko                                          org.winehq.Wine.gecko                                                     stable-22.08          system
Mono                                           org.winehq.Wine.mono                                                      stable-21.08          system
Mono                                           org.winehq.Wine.mono                                                      stable-22.08          system
```


Discussion: See also #2718 
@Friendly-Banana that bug doesn't mention that all Nvidia packages are updated every time an app updates, only that Flatpak doesn't clean up when installing a new one, and also shows different behaviour with the `--unused` flag compared to what I'm seeing. 
Will it actually cause problems if I remove some of the older nvidia runtimes? Flatpak says every single one of them is used by every single Flatpak app I have installed, so will it try to uninstall those programs that "depend" on the old runtimes or will they stop working because of missing the dependency?

Why is there no explanation for this?
See https://github.com/flathub/org.freedesktop.Platform.GL.nvidia/issues/42
@Mailaender that bug does not cover all aspects of my issue, and is on the Nvidia package while my issue is related to the behaviour of Flatpak itself rather than the behaviour or performance of the Nvidia package.
Added the output from `flatpak list --runtime` to the **Additional Information** part of my original issue post.
I'm getting the same issue on ArchLinux (EndeavourOS). If I run `flatpak remove --unused` I get:
```
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-535-54-03 branch 1.4:
   com.brave.Browser, net.runelite.RuneLite, com.github.tchx84.Flatseal, de.haeckerfelix.Shortwave, org.kde.tokodon
Really remove? [y/n]: n
Info: applications using the extension org.freedesktop.Platform.GL.nvidia-530-41-03 branch 1.4:
   com.brave.Browser, net.runelite.RuneLite, com.github.tchx84.Flatseal, de.haeckerfelix.Shortwave, org.kde.tokodon
Really remove? [y/n]: n
```
Output from `flatpak list --runtime | grep -i nvidia` is:
```
nvidia-530-41-03	org.freedesktop.Platform.GL.nvidia-530-41-03		1.4	system
nvidia-535-54-03	org.freedesktop.Platform.GL.nvidia-535-54-03		1.4	system
nvidia-535-86-05	org.freedesktop.Platform.GL.nvidia-535-86-05		1.4	system
```

Running Flatpak v1.15.4, kernel 6.4.4-arch1-1.
I am also still getting this behaviour on flatpak 1.15.4 on Manjaro with kernel 6.4.3-1. 

Further, even when uninstalling the runtimes via `--unused` despite it explicitly saying they're not actually unused, it only removes the `.GL.` versions and the `.GL32.` versions have to then be removed manually.



Fix (patch):
diff --git a/app/flatpak-builtins-uninstall.c b/app/flatpak-builtins-uninstall.c
index 78143908db..d8e5bb637e 100644
--- a/app/flatpak-builtins-uninstall.c
+++ b/app/flatpak-builtins-uninstall.c
@@ -345,7 +345,7 @@ flatpak_builtin_uninstall (int argc, char **argv, GCancellable *cancellable, GEr
 
           udir = uninstall_dir_ensure (uninstall_dirs, dir);
 
-          unused = flatpak_dir_list_unused_refs (dir, opt_arch, NULL, NULL, NULL, FALSE, cancellable, error);
+          unused = flatpak_dir_list_unused_refs (dir, opt_arch, NULL, NULL, NULL, FLATPAK_DIR_FILTER_NONE, cancellable, error);
           if (unused == NULL)
             return FALSE;
 
diff --git a/common/flatpak-dir-private.h b/common/flatpak-dir-private.h
index 2f7cf89718..df006d1fce 100644
--- a/common/flatpak-dir-private.h
+++ b/common/flatpak-dir-private.h
@@ -360,6 +360,12 @@ typedef enum {
   FLATPAK_DIR_STORAGE_TYPE_NETWORK,
 } FlatpakDirStorageType;
 
+typedef enum {
+  FLATPAK_DIR_FILTER_NONE = 0,
+  FLATPAK_DIR_FILTER_EOL = 1 << 0,
+  FLATPAK_DIR_FILTER_AUTOPRUNE = 1 << 1,
+} FlatpakDirFilterFlags;
+
 typedef enum {
   FIND_MATCHING_REFS_FLAGS_NONE = 0,
   FIND_MATCHING_REFS_FLAGS_FUZZY = (1 << 0),
@@ -1049,7 +1055,7 @@ char **               flatpak_dir_list_unused_refs                          (Fla
                                                                              GHashTable                    *metadata_injection,
                                                                              GHashTable                    *eol_injection,
                                                                              const char * const            *refs_to_exclude,
-                                                                             gboolean                       filter_by_eol,
+                                                                             FlatpakDirFilterFlags          filter_flags,
                                                                              GCancellable                  *cancellable,
                                                                              GError                       **error);
 
diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c
index f6a2f872a3..5d9ee91ba6 100644
--- a/common/flatpak-dir.c
+++ b/common/flatpak-dir.c
@@ -16491,6 +16491,7 @@ find_used_refs (FlatpakDir         *self,
                 GHashTable         *metadata_injection,
                 GHashTable         *refs_to_exclude,
                 GHashTable         *used_refs, /* This is filled in */
+                GHashTable         *autopruned_refs, /* This is filled in */
                 GCancellable       *cancellable,
                 GError            **error)
 {
@@ -16662,6 +16663,10 @@ find_used_refs (FlatpakDir         *self,
                        flatpak_decomposed_get_ref (ref_to_analyze), dir_name);
               queue_ref_for_analysis (rel->ref, arch, analyzed_refs, refs_to_analyze);
             }
+          else
+            {
+              g_hash_table_add (autopruned_refs, flatpak_decomposed_ref (rel->ref));
+            }
         }
     }
 
@@ -16672,19 +16677,22 @@ find_used_refs (FlatpakDir         *self,
  * flatpak_installation_list_unused_refs_with_options().
  * The returned pointer array is transfer full. */
 char **
-flatpak_dir_list_unused_refs (FlatpakDir         *self,
-                              const char         *arch,
-                              GHashTable         *metadata_injection,
-                              GHashTable         *eol_injection,
-                              const char * const *refs_to_exclude,
-                              gboolean            filter_by_eol,
-                              GCancellable       *cancellable,
-                              GError            **error)
+flatpak_dir_list_unused_refs (FlatpakDir            *self,
+                              const char            *arch,
+                              GHashTable            *metadata_injection,
+                              GHashTable            *eol_injection,
+                              const char * const    *refs_to_exclude,
+                              FlatpakDirFilterFlags  filter_flags,
+                              GCancellable          *cancellable,
+                              GError               **error)
 {
   g_autoptr(GHashTable) used_refs = NULL;
+  g_autoptr(GHashTable) autoprune_refs = NULL;
   g_autoptr(GHashTable) excluded_refs_ht = NULL;
   g_autoptr(GPtrArray) refs =  NULL;
   g_autoptr(GPtrArray) runtime_refs = NULL;
+  gboolean filter_by_eol = (filter_flags & FLATPAK_DIR_FILTER_EOL) != 0;
+  gboolean filter_by_autoprune = (filter_flags & FLATPAK_DIR_FILTER_AUTOPRUNE) != 0;
 
   /* Convert refs_to_exclude to hashtable for fast repeated lookups */
   if (refs_to_exclude)
@@ -16700,12 +16708,14 @@ flatpak_dir_list_unused_refs (FlatpakDir         *self,
     }
 
   used_refs = g_hash_table_new_full ((GHashFunc)flatpak_decomposed_hash, (GEqualFunc)flatpak_decomposed_equal, (GDestroyNotify)flatpak_decomposed_unref, NULL);
+  autoprune_refs = g_hash_table_new_full ((GHashFunc)flatpak_decomposed_hash, (GEqualFunc)flatpak_decomposed_equal, (GDestroyNotify)flatpak_decomposed_unref, NULL);
 
-  g_info ("Checking installation ‘%s’ %s",
+  g_info ("Checking installation ‘%s’ %s%s",
           flatpak_dir_get_name_cached (self),
-          filter_by_eol ? "for EOL unused refs" : "for unused refs");
+          filter_by_eol ? "for EOL unused refs" : "for unused refs",
+          filter_by_autoprune ? " and autoprunes" : "");
   if (!find_used_refs (self, NULL, arch, metadata_injection, excluded_refs_ht,
-                       used_refs, cancellable, error))
+                       used_refs, autoprune_refs, cancellable, error))
     return NULL;
 
   /* If @self is a system installation, also check the per-user installation
@@ -16722,7 +16732,7 @@ flatpak_dir_list_unused_refs (FlatpakDir         *self,
       g_info ("Checking installation ‘%s’ by checking for dependent refs in ‘%s’",
               flatpak_dir_get_name_cached (self), flatpak_dir_get_name_cached (user_dir));
       if (!find_used_refs (self, user_dir, arch, metadata_injection, excluded_refs_ht,
-                           used_refs, cancellable, &local_error))
+                           used_refs, autoprune_refs, cancellable, &local_error))
         {
           /* We may get permission denied if the process is sandboxed with
            * systemd's ProtectHome=
@@ -16752,9 +16762,10 @@ flatpak_dir_list_unused_refs (FlatpakDir         *self,
       if (arch != NULL && !flatpak_decomposed_is_arch (ref, arch))
         continue;
 
-      if (filter_by_eol)
+      if (filter_flags)
         {
           gboolean is_eol = FALSE;
+          gboolean is_autopruned = g_hash_table_contains (autoprune_refs, ref);
 
           if (eol_injection && g_hash_table_contains (eol_injection, flatpak_decomposed_get_ref (ref)))
             {
@@ -16772,11 +16783,12 @@ flatpak_dir_list_unused_refs (FlatpakDir         *self,
                  flatpak_deploy_data_get_eol_rebase (deploy_data));
             }
 
-          if (!is_eol)
+          if (!((is_autopruned && filter_by_autoprune) || (is_eol && filter_by_eol)))
             {
-              g_debug ("%s: Ref %s (%s) not end-of-life, so excluding from EOL unused refs",
+              g_debug ("%s: Ref %s (%s) not %s, so excluding from unused refs",
                        G_STRFUNC, flatpak_decomposed_get_ref (ref),
-                       flatpak_dir_get_name_cached (self));
+                       flatpak_dir_get_name_cached (self),
+                       (!is_eol && filter_by_eol) ? "end-of-life" : "autopruned");
               continue;
             }
         }
diff --git a/common/flatpak-installation.c b/common/flatpak-installation.c
index 8b36355237..d44a633c8d 100644
--- a/common/flatpak-installation.c
+++ b/common/flatpak-installation.c
@@ -3071,10 +3071,11 @@ flatpak_installation_list_unused_refs (FlatpakInstallation *self,
  *
  *   * exclude-refs (as): Act as if these refs are not installed even if they
  *       are when determining the set of unused refs
- *   * filter-by-eol (b): Only return refs as unused if they are End-Of-Life.
- *       Note that if this option is combined with other filters (of which there
- *       are none currently) non-EOL refs may also be returned.
- *
+ *   * filter-by-eol (b): Return refs as unused if they are End-Of-Life.
+ *       Note that if this option is combined with other filters then non-EOL refs may also be returned.
+ *   * filter-by-autoprune (b): Return refs as unused if they should be autopruned.
+ *       Note that if this option is combined with other filters then non-autoprune refs may also be returned.
+
  * Returns: (transfer container) (element-type FlatpakInstalledRef): a GPtrArray of
  *   #FlatpakInstalledRef instances
  *
@@ -3093,19 +3094,28 @@ flatpak_installation_list_unused_refs_with_options (FlatpakInstallation *self,
   g_auto(GStrv) refs_strv = NULL;
   g_autofree char **refs_to_exclude = NULL;
   gboolean filter_by_eol = FALSE;
+  gboolean filter_by_autoprune = FALSE;
+  FlatpakDirFilterFlags filter_flags = FLATPAK_DIR_FILTER_NONE;
 
   if (options)
     {
       (void) g_variant_lookup (options, "exclude-refs", "^a&s", &refs_to_exclude);
       (void) g_variant_lookup (options, "filter-by-eol", "b", &filter_by_eol);
+      (void) g_variant_lookup (options, "filter-by-autoprune", "b", &filter_by_autoprune);
     }
 
   dir = flatpak_installation_get_dir (self, error);
   if (dir == NULL)
     return NULL;
 
+  if (filter_by_eol)
+    filter_flags |= FLATPAK_DIR_FILTER_EOL;
+  if (filter_by_autoprune)
+    filter_flags |= FLATPAK_DIR_FILTER_AUTOPRUNE;
+
   refs_strv = flatpak_dir_list_unused_refs (dir, arch, metadata_injection, NULL,
-                                            (const char * const *)refs_to_exclude, filter_by_eol,
+                                            (const char * const *)refs_to_exclude,
+                                            filter_flags,
                                             cancellable, error);
   if (refs_strv == NULL)
     return NULL;
diff --git a/common/flatpak-transaction.c b/common/flatpak-transaction.c
index 0d7a944d09..86f866f926 100644
--- a/common/flatpak-transaction.c
+++ b/common/flatpak-transaction.c
@@ -24,6 +24,7 @@
 #include <glib/gi18n-lib.h>
 
 #include "flatpak-auth-private.h"
+#include "flatpak-dir-private.h"
 #include "flatpak-error.h"
 #include "flatpak-installation-private.h"
 #include "flatpak-progress-private.h"
@@ -4999,7 +5000,7 @@ add_uninstall_unused_ops (FlatpakTransaction  *self,
                                                       NULL, /* metadata_injection */
                                                       NULL, /* eol_injection */
                                                       NULL, /* exclude_refs */
-                                                      TRUE, /* filter_by_eol */
+                                                      FLATPAK_DIR_FILTER_EOL | FLATPAK_DIR_FILTER_AUTOPRUNE,
                                                       cancellable, error);
       if (old_unused_refs == NULL)
         return FALSE;
@@ -5056,7 +5057,7 @@ add_uninstall_unused_ops (FlatpakTransaction  *self,
                                               metadata_injection,
                                               eol_injection,
                                               to_be_excluded_strv,
-                                              TRUE, /* filter_by_eol */
+                                              FLATPAK_DIR_FILTER_EOL | FLATPAK_DIR_FILTER_AUTOPRUNE,
                                               cancellable, error);
   if (unused_refs == NULL)
     return FALSE;
diff --git a/doc/flatpak-metadata.xml b/doc/flatpak-metadata.xml
index c9d9f1ebba..a7f00da151 100644
--- a/doc/flatpak-metadata.xml
+++ b/doc/flatpak-metadata.xml
@@ -925,10 +925,10 @@
                     <term><option>autoprune-unless</option> (string)</term>
                     <listitem><para>
                         A condition that must be false for the extension to be considered unused when
-                        pruning. For example, <command>flatpak uninstall --unused</command> uses
-                        this information. The only currently recognized value is active-gl-driver,
-                        which is true if the name of the active GL driver matches the extension
-                        point basename. Available since 0.11.8.
+                        pruning. For example, <command>flatpak uninstall --unused</command> and
+                        <command>flatpak update</command> use this information. The only currently
+                        recognized value is active-gl-driver, which is true if the name of the active
+                        GL driver matches the extension point basename. Available since 0.11.8.
                     </para></listitem>
                 </varlistentry>
                 <varlistentry>