How to remove unused MacPorts packages? Whenever I install a program via MacPorts, it gets and installs lots of dependencies. When I uninstall said program, how do I make MacPorts recursively remove all unused dependencies with it?
To avoid accidentally cutting leaves that might be things you requested you should install the port_cutleaves package then run sudo port_cutleaves
https://guide.macports.org/#using.common-tasks.keeplean



MacPorts 1.9.0 added the sqlite portdbformat and MacPorts 2.0.0 dropped the old flat format. The sqlite port DB format is the default for new installations; old installations that were upgraded to 1.9.x will continue to use the flat format1. You can convert a 1.9.x flat system to the sqlite format by upgrading to MacPorts 2.0 or by changing the portdbformat value in  /opt/local/etc/macports/macports.conf and then issuing a port command as root (e.g. sudo port installed).

One of the features of the new format is that it keeps track of “requested” versus “unrequested” port installations. An unrequested port is one that was only installed because some other port depends on it. The leaves pseudo-portname expands to all the unrequested ports upon which no other installed port depends. You can use this to “clean up” unneeded ports even if you did not originally uninstall them with sudo port uninstall --follow-dependencies portname (which will do what you want, but only if you remember to use it every time you uninstall something).



You should examine your existing leaves before uninstalling any of them.

port echo leaves


Some common leaves (automake, gperf, libtool, pkgconfig) are build-time dependencies of common ports, so you may want to “request” them (sudo port setrequested port1 port2 port3 …) to avoid uninstalling them just to have to reinstall them later.

You can uninstall any remaining leaves quite easily:

sudo port uninstall leaves




Note: Before pruning your leaves, you may also want to uninstall old versions of ports that are no longer “active”. This may reveal a few more leaves (i.e. ports that are dependencies of ports that are installed, but inactive):

sudo port uninstall inactive




There are several sections in the MacPorts Guide that also describe the process of using leaves to uninstall unneeded ports.