14

Does anyone know why the directory at $HOME/.local/share/flatpak/repo contains so many files (69 k) and is so huge (2 Gb) ? What is that ? I hardly ever use flatpaks anyway....

I guess it is not important to include the flatpak directory in one's backup (deja-dup).

PS it looks like there are hard links inside because the flatpak folder has got twice as many files but the same total size.

Johannes Lemonde
  • 1,561
  • 1
  • 11
  • 20
  • 1
    I have a single object file 0c that is 1.1G ! – xgdgsc Oct 08 '18 at 04:26
  • Did you install things with `--user`? Provide the output of `flatpak list --show-details --app --runtime` to list install details (all columns) of all apps and runtimes. Run `sudo flatpak uninstall --unused` (`--user` to just user-wide) to uninstall unused references (`du -sh ~/.local/share/flatpak/repo` to test before/after). `repair --user` prune objects. BTW, you can always clean cache (`sudo rm -rfv /var/tmp/flatpak-cache-* ~/.cache/flatpak/**`) – Pablo Bianchi May 09 '22 at 18:59
  • @PabloBianchi that list command is great. Is it possible to see what depends on a package? I've got half a dozen `org.gnome.Platform` versions taking up a~1GB each but hardly any actual apps that might need those. – Oli May 12 '22 at 09:31
  • 1
    @Oli That's one of the [available runtimes](https://docs.flatpak.org/en/latest/available-runtimes.html), the [basic dependency for apps](https://docs.flatpak.org/en/latest/basic-concepts.html). You can use `flatpak list --app --columns=application,runtime` to list which app use which runtime, or `--app-runtime=` to list apps using a specific runtime. `flatpak uninstall –unused` should remove (both system and user) unused runtimes. You have flatpak [older than 1.9.1](https://blogs.gnome.org/mwleeds/2021/01/11/cleaning-up-unused-flatpak-runtimes/)? – Pablo Bianchi May 12 '22 at 16:04

2 Answers2

10

Flatpak packages runs in sandbox mode: so, by design, flatpak takes a considerable amount of disk space for an individual application, even if it is a smaller one, because it pulls all dependencies for an app and runs independently. The advantage of this design is that you don't need to worry about dependencies and updates of the system that may break your app. However, it takes up huge amount of disk space.

As a consequence, the only thing that a user can do is to monitor flatpak size and perform a periodic clean. Personally, I have a script called "spring-cleaning" that I use periodically to perform a cleaning of the system (and within it there is a section related to flatpak).

Generic cleanup

  • Flatpak apps may share some runtime packages, that represent the basic dependencies for apps: if a runtime is installed in your system but is not used by any flatpak app (flatpak list --app --columns=application,runtime lists which app use which runtime), you can remove it with flatpak uninstall --unused. This is a cleanup that may have a great effect the first time you run it, but as you can imagine the cleanup may not be significative after the first use.
  • Flatpak apps may create objects in the /var/lib/flatpak/repo/objects folder, that may reach a significative size. To prune these objects (that don't have an explicit reference to their app), you need to run (with sudo, because you are cleaning objects in a system folder) sudo flatpak repair. The impact of this cleanup may be significative every time you run the command.
  • Flatpak cache files may be removed as well, in both system and user folders. You can run sudo rm -rfv /var/tmp/flatpak-cache-* and rm -rfv ~/.cache/flatpak/*

Specific cleanup

  • You can uninstall the app that you don't need, or replace the huge ones with the .deb version of the app, if feasible. You can sort flatpak apps by size flatpak --columns=name,size --user list and then uninstall an app using its name flatpak uninstall <flatpak_app_name>

Combining in a script (to run with sudo)

#!/bin/bash

# Error status variables
STATUS_OK=0
STATUS_ERROR=1

# Definitions
USER_NAME="${SUDO_USER:-${USER}}"
USER_DIR="/home/${USER_NAME}"

# Execute it as root user
if [ "${USER}" != root ]; then
  echo "ERROR: must be root! Exiting..."
  exit "${STATUS_ERROR}"
fi

# Current status
USED_BEFORE="$(df -k / | awk 'NR>1 {print $3}')"

# flatpak cleanup
if [ -n "$(command -v flatpak)" ]; then
  flatpak repair &> /dev/null
  # Remove flatpak cache
  rm -rf /var/tmp/flatpak-cache-*
  sudo -u "${USER_NAME}" rm -rf "${USER_DIR}"/.cache/flatpak/*
  # Remove unused flatpak runtimes
  if [ -n "$(flatpak list --runtime)" ]; then
    flatpak uninstall --unused
  fi
fi

# Current status
USED_AFTER="$(df -k / | awk 'NR>1 {print $3}')"

# Summary
echo "Freed up space: $(( (USED_BEFORE - USED_AFTER)/1024 )) MB"
exit "${STATUS_OK}"
Lorenz Keel
  • 8,362
  • 8
  • 36
  • 49
  • 1
    Excellent answer and handy script! Just one note: while there is nothing wrong in this script, it is recommended not to use all caps for the variables' names so that you (anyone) won't accidentally override any environmental or internal variables, which by convention use all caps. You can read more in these posts: [Are there naming conventions for variables in shell scripts?](https://unix.stackexchange.com/q/42847/322816) and [Correct Bash and shell script variable capitalization](https://stackoverflow.com/q/673055/3140225) – BeastOfCaerbannog Jun 16 '22 at 07:44
  • 1
    Thanks for your positive feedback :-) I'm aware of what you are observing in my script, let's say that I use this convention because I want to distinguish immediately the variables in my scripts. That's also the reason why I use curly brackets, that are not mandatory. – Lorenz Keel Jun 16 '22 at 08:04
2

Flatpaks are "sandboxed", meaning that any app caches, large libraries, etc. that the app needs are stored in those directories. This means that the file sizes for these directories are often horribly large.

Pablo Bianchi
  • 14,308
  • 4
  • 74
  • 117
Aarav Garg
  • 118
  • 10