Skip to the content
pacman-master.com

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.

cautionsudo pacman -D --asdeps firefox

Nothing on disk changes here. What changes is the answer a later -Qdt gives about this package.

Listing them

CommandWhat it gives youLevel
pacman -QdtThe orphans, with versions and descriptionsroutine
pacman -QdtqThe same list, names only, ready to hand to another commandroutine
pacman -QetThe other half: packages installed explicitly that nothing requires, which is a list of what you actually choseroutine
pacman -Qi <package>Why one package is there, including whether it was installed as a dependency and what requires it nowroutine

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:

Why an orphan sweep takes more than one roundThree columns, one per round of the sweep that follows removing app-main without -Rs. Round one lists libfoo and oldlib, which nothing requires any more. Removing those leaves libz with nothing requiring it, so round two lists libz. Round three finds nothing and the sweep stops. Underneath, the total across the rounds is given beside pacman -Qdt with the level the explainer gives it.After sudo pacman -R app-main, the sweep runs in roundsround onelibfoonothing requires it nowoldlibnothing requires it now2removedround twolibznothing requires it now1removedround threenothing left to listthe sweep stops3packages leave over two roundspacman -QdtroutineTaking one away can uncover the next, so the listing is worth repeating.
Taking one orphan away can make the next one visible, so the listing is worth repeating until it comes back empty.
RoundWhat -Qdt listsWhy
Before the removal1One package was already an orphan: nothing had required it for some time
Round one2The removal took away the only thing that required these
Round two1Removing round one took away the only thing that required this
Round three0Nothing is left to list, and the sweep stops
Total removed3Across 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:

routinepacman -Qdt
not pacmansudo 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.

How to check this page

Two commands on your own machine: pacman -<operation> --help lists the modifiers that operation accepts, and man pacman opens the pacman(8) manual page these descriptions are written from. Where the descriptions come from, and what this site is not, is set out on the about page.

This page was last edited on .