paccache: trimming the cache by version count
pacman has two cleaning commands and they are blunt: one keeps the version of a package that is installed and deletes the rest, the other empties the cache. paccache adds the question people usually mean, which is how many versions back they want to be able to go.
paccache -rk2
What it does. Keep the two newest cached versions of each package and delete anything older, without touching what is installed.
Watch out. paccache is not part of pacman. It ships in a separate package, so on a machine that has not installed that package the command does not exist.
The three cleaners side by side
Take a cache holding 6 files for three packages, two of which are still installed:
| Command | What it goes by | Kept | Deleted |
|---|---|---|---|
sudo pacman -Sc | Whether the cached file is the version that is installed | 2 | 4 |
sudo pacman -Scc | Nothing; the cache is emptied | 0 | 6 |
paccache -rk2 | How many versions of each package you want to keep | 5 | 1 |
paccache -ruk0 | Whether the package is installed, versions ignored | 5 | 1 |
The first and third rows are the ones worth reading together, and -Sc is the
harsher of the two against a package you still have. On this cache -Sc deletes 4
files and paccache -rk2 deletes 1: -Sc leaves one file per installed
package, the version the local database records, while -rk2 leaves the two newest
of each name. What paccache adds is the number, not a softer default. The row
that matches the older reading of -Sc is paccache -ruk0, which goes
by the package name and ignores versions.
The counts above are cache files only. Outside the cache -Sc also deletes the sync databases of repositories that are no longer configured,
which live in /var/lib/pacman/sync and are fetched again by the next
refresh.
The flags that matter
| Flag | What it does |
|---|---|
-d | Print what would go without deleting anything, which is the flag to use first |
-r | Remove, as opposed to moving the files somewhere else |
-k <n> | Keep the newest n versions of each package; -k0 keeps none |
-u | Work only on packages that are not installed any more |
-m <dir> | Move the files into a directory instead of deleting them |
The flags are paccache's own, not pacman's, so they do not follow the operation-letter shape the rest of this site describes and they are documented by paccache rather than by pacman(8).
paccache -dk2A dry run: this prints what a -rk2 would take and removes nothing.
paccache -rk2paccache -ruk0Removes the cached files of packages that are no longer installed, whatever their version.
Getting it
paccache comes in the pacman-contrib package, which is not
installed by default:
sudo pacman -S pacman-contribThe hook machinery belongs to pacman rather than to this package: libalpm runs
.hook files from /etc/pacman.d/hooks before or after a
transaction, and a hook that trims the cache is one you write yourself, because
pacman-contrib ships no hook file. What it does ship is a systemd
timer, paccache.timer, which once enabled cleans the cache weekly
with paccache's default options. Either way is worth thinking about twice: it
makes the deletion silent, and the cached file you wanted back is the one you did
not notice going.
Keeping at least one older version of each package is what makes going back possible without a download. A cache trimmed to -k0 has the same practical effect as an empty one for that purpose, even though the command reads as gentler.
Knowing how much is there first
Before deciding how many versions to keep, it is worth knowing what the cache weighs. That is not a pacman question either:
du -sh /var/cache/pacman/pkgThe cache page covers what pacman's own two commands do, and the downgrade page covers the thing the cache exists for.
Common questions
What does paccache do?
It deletes cached package files, keeping the newest few versions of each package. The number kept is set with -k, so paccache -rk2 leaves the two newest versions of each package on disk.
Is paccache part of pacman?
No. It ships in the pacman-contrib package, which is not installed by default. Its flags are its own and are documented by paccache rather than by the pacman manual page.
What is the difference between paccache and pacman -Sc?
pacman -Sc deletes the cached files of packages that are no longer installed, and the sync databases of repositories that are no longer configured. CleanMethod in /etc/pacman.conf decides which cached file counts as installed. Its default, KeepInstalled, goes by the local database, which records the version that is installed. KeepCurrent goes by the sync databases instead, so the file that survives is the one the repositories offer at that moment. paccache goes by version count instead, so paccache -rk2 leaves the two newest versions of a package that is still installed.
How do I see what paccache would delete before it does?
Run it with -d instead of -r. That prints the files a matching -r would take and removes nothing, which makes it the flag to try a retention number with.