DEV Community

Nurlan
Nurlan

Posted on

How to Find and Clean Up Unowned `__pycache__` Directories on Arch Linux

When you install or run Python-based tools on Arch Linux — especially from the AUR — you might notice that your system accumulates a lot of __pycache__ directories. These are generated when Python byte-compiles modules, but they aren’t always removed when you uninstall a package.

In this short guide, we’ll walk through how to identify and optionally clean up those __pycache__ directories — specifically the ones not owned by any package — and exclude system paths like Flatpak while we're at it.


Why Does This Happen?

Tools installed via the AUR (like waydroid-scripts-git, for example) might drop files under /opt, /usr/local, or other places. These files (especially .py and __pycache__) might not be tracked by pacman, so even after uninstalling the tool, leftover Python cache directories can remain.


Finding Unowned __pycache__ Directories

Here’s a one-liner that will:

  • Search your system for all __pycache__ directories
  • Skip the Flatpak directory (/var/lib/flatpak)
  • Check whether each is owned by any package
  • Print only the ones that are not owned
find / -type d -name '__pycache__' -not -path '/var/lib/flatpak/*' 2>/dev/null | while read dir; do
  pacman -Qo "$dir" &>/dev/null || echo "Not owned: $dir"
done
Enter fullscreen mode Exit fullscreen mode

Output Example

Not owned: /opt/waydroid-scripts/tools/__pycache__
Not owned: /usr/local/lib/python3.11/site-packages/some_tool/__pycache__
Enter fullscreen mode Exit fullscreen mode

Conclusion

__pycache__ directories aren't dangerous, but they can accumulate and clutter your system — especially in directories that aren’t cleaned by package managers. With a few commands, you can find and clean up these leftovers safely, keeping your system neat and under control.

Have your own cleanup tricks? Share them in the comments!

Top comments (0)