How to remove unused Homebrew dependencies

Homebrew tracks whether each formula was installed directly or only as another formula's dependency. After package removals, upgrades, or Brewfile cleanup, dependency-only formulae can remain in the prefix even though no installed formula or cask still needs them.

The brew autoremove command removes formulae that Homebrew considers dependency-installed and no longer required. The safe review path starts with brew leaves and brew autoremove --dry-run so the removal list is visible before Homebrew deletes any kegs.

An empty dry run is a valid result when there are no unneeded dependency formulae. If a listed formula should stay available as a standalone tool, stop and review why Homebrew still treats it as dependency-installed instead of treating dependency cleanup as a cache cleanup command.

Steps to remove unused Homebrew dependencies:

  1. List formulae that Homebrew did not mark as installed on request.
    $ brew list --no-installed-on-request
    bubblewrap
    libcap
    libidn2
    libunistring

    This list can include dependency formulae that are still required by installed packages. Use it as a starting inventory, not as the removal list.

  2. List dependency-installed leaves.
    $ brew leaves --installed-as-dependency
    libidn2

    A dependency leaf is installed as a dependency and is not required by another installed formula or cask. It is a strong cleanup candidate, but the dry run is still the removal authority.

  3. Preview the formulae that brew autoremove would remove.
    $ brew autoremove --dry-run
    ==> Would autoremove 4 unneeded formulae:
    bubblewrap
    libcap
    libidn2
    libunistring
  4. Apply the reviewed autoremove plan.
    $ brew autoremove
    ==> Autoremoving 4 unneeded formulae:
    bubblewrap
    libcap
    libidn2
    libunistring
    Uninstalling /home/linuxbrew/.linuxbrew/Cellar/libidn2/2.3.8... (81 files, 1.3MB)
    Uninstalling /home/linuxbrew/.linuxbrew/Cellar/libunistring/1.4.2... (60 files, 6.6MB)
    Uninstalling /home/linuxbrew/.linuxbrew/Cellar/bubblewrap/0.11.2... (10 files, 377.3KB)
    Uninstalling /home/linuxbrew/.linuxbrew/Cellar/libcap/2.78... (103 files, 1MB)

    Do not run brew autoremove until the dry-run list contains only dependency formulae that can be removed. This command uninstalls packages from the active Homebrew prefix.

  5. Check for remaining dependency leaves.
    $ brew leaves --installed-as-dependency

    No output means Homebrew has no dependency-installed leaves left to remove. Dependency formulae that are still required by installed packages remain in place.