Orphan packages and how to sweep them
pacman records why each package is on the machine: because somebody asked for it, or because something else needed it. An orphan is a package in the second group whose reason has since been removed, and it is the only kind of package that is safe to sweep out without naming.
pacman -Qdt
-Q- --query — ask the local database about packages that are already installed
-d- --deps — restrict the list to packages that were installed as dependencies
-t- --unrequired — restrict the list to packages no installed package requires
Runs without root.
What it does. List the packages that were installed as dependencies and that nothing installed requires any longer.
Watch out. Removing one orphan can turn its own dependencies into orphans, so the listing is worth running again until it comes back empty.
The record that makes this possible
An explicitly installed package is one that was named on a pacman command line. It stays on the machine until it is named again.
A dependency is a package pacman pulled in because something else required it. Once nothing requires it, it is an orphan.
The record is written at install time and can be changed afterwards with -D,
which is what makes -D a database operation with consequences rather than a
harmless edit. Marking something as a dependency is enough to make a later sweep
take it away without naming it.
sudo pacman -D --asdeps firefoxNothing on disk changes here. What changes is the answer a later -Qdt gives about this package.
Listing them
| Command | What it gives you | Level |
|---|---|---|
pacman -Qdt | The orphans, with versions and descriptions | routine |
pacman -Qdtq | The same list, names only, ready to hand to another command | routine |
pacman -Qet | The other half: packages installed explicitly that nothing requires, which is a list of what you actually chose | routine |
pacman -Qi <package> | Why one package is there, including whether it was installed as a dependency and what requires it now | routine |
None of those write anything, which is why none of them need root. Reading first is the whole point: the list is short enough to look at, and a name you recognise on it is usually a package you meant to keep.
Why one pass is not enough
Take a small system where app-main has just been removed with a
plain -R, which takes the name and nothing else:
| Round | What -Qdt lists | Why |
|---|---|---|
| Before the removal | 1 | One package was already an orphan: nothing had required it for some time |
| Round one | 2 | The removal took away the only thing that required these |
| Round two | 1 | Removing round one took away the only thing that required this |
| Round three | 0 | Nothing is left to list, and the sweep stops |
| Total removed | 3 | Across 2 rounds of listing and removing |
This is why a sweep is a loop rather than a command. It is also why -Rs reaches
further than a plain removal: it follows the chain at removal time instead of
leaving it to be discovered later.
Doing the sweep
Look at the list first, then remove what it named:
pacman -Qdtsudo pacman -Rns $(pacman -Qdtq)A shell substitution rather than a single pacman command, so it carries no level here: the shell builds the list and pacman receives whatever it produced. On a machine with no orphans the substitution is empty and the removal is refused for want of a target.
A package you installed on purpose but that was recorded as a dependency looks exactly like an orphan from the outside. Running pacman -D --asexplicit <package> on it first is what keeps a sweep from taking it.
Common questions
What is an orphan package in pacman?
It is a package that was installed as a dependency of something else and that nothing installed requires any more. pacman keeps that reason in its local database, which is what makes the list possible.
How do I list orphan packages on Arch Linux?
Run pacman -Qdt. The d limits the answer to packages installed as dependencies and the t to those nothing requires. Adding q gives the same list as bare names.
Why does pacman -Qdt show new orphans after I remove some?
Removing an orphan can take away the only thing that required its own dependencies, which makes those orphans in turn. The listing is worth repeating until it comes back empty.
Is it safe to remove every orphan pacman lists?
Usually, but read the list first. A package you chose yourself can appear there if it was recorded as a dependency, and marking it with pacman -D --asexplicit is what keeps a later sweep from taking it.