Skip to the content
pacman-master.com

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:

What each cleaning command leaves in the package cacheA worked cache holding six package files down the left, with the two that are the installed versions marked as in use. Three columns to the right show the same six files after pacman -Sc, after pacman -Scc and after paccache -rk2, each file marked kept or deleted, with the two totals under each column./var/cache/pacman/pkgfirefox-141.0-1firefox-142.0-1firefox-143.0-1in usevim-9.1.1-1vim-9.1.2-1in useoldpkg-2.0-16files in the cachepacman -Scdeleteddeletedkeptdeletedkeptdeleted2kept4deletedpacman -Sccdeleteddeleteddeleteddeleteddeleteddeleted0kept6deletedpaccache -rk2deletedkeptkeptkeptkeptkept5kept1deleted
-Sc goes by whether the cached file is the version that is installed, and takes the sync databases of repositories that are no longer configured with it. paccache goes by how many versions you want to keep, so -rk2 leaves more of an installed package behind than -Sc does.
CommandWhat it goes byKeptDeleted
sudo pacman -ScWhether the cached file is the version that is installed24
sudo pacman -SccNothing; the cache is emptied06
paccache -rk2How many versions of each package you want to keep51
paccache -ruk0Whether the package is installed, versions ignored51

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

FlagWhat it does
-dPrint what would go without deleting anything, which is the flag to use first
-rRemove, as opposed to moving the files somewhere else
-k <n>Keep the newest n versions of each package; -k0 keeps none
-uWork 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).

not pacmanpaccache -dk2

A dry run: this prints what a -rk2 would take and removes nothing.

not pacmanpaccache -rk2
not pacmanpaccache -ruk0

Removes 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:

routinesudo pacman -S pacman-contrib

The 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:

not pacmandu -sh /var/cache/pacman/pkg

The 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.

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 .