Uninstall a tool with Homebrew and the libraries it pulled in stay behind. Over time that adds up.
Homebrew remembers which formulae you asked for and which came along as dependencies. autoremove removes the dependencies that nothing needs anymore. I’d always look first:
brew autoremove -n
-n, long form --dry-run, lists what would be uninstalled and changes nothing. If the list looks right, run it for real:
brew autoremove
brew cleanup
cleanup then deletes old downloads and outdated versions from the cache, which often frees more space than the packages themselves.
The list is only as good as Homebrew’s idea of what you installed on purpose. If you once used a library directly, say openssl@3 for a build script, but it was installed as a dependency, it shows up here and gets removed. In that case mark it as yours with brew tab --installed-on-request openssl@3, and autoremove leaves it alone. Anything you compiled yourself against those libraries is invisible to Homebrew, so a quick test of your own tools after a cleanup is wise.
Further reading: brew manpage (Homebrew docs)