pacman -Sc: clearing the package cache
To clear the package cache, run sudo pacman -Sc. It deletes the cached files of packages that are no longer installed, and the sync databases of repositories that are no longer configured. What the local database records is the version a package is installed at, so the file kept for a package still on the machine is that one version and older files for it go with the rest.
sudo pacman -Sc
-S- --sync — install packages from the repositories and upgrade the system
-c- --clean — remove the cached files of packages that are no longer installed, and the sync databases of repositories that are no longer configured
Needs root.
What it does. Delete the cached files of packages that are no longer installed, and the sync databases of repositories that are no longer configured.
Watch out. The cache is what lets you reinstall or step back to a previous version without a network; deleting it is not free.
Step by step
- See how much is actually there
The cache is a plain directory, so the size can be read before anything is deleted. du is part of coreutils, not of pacman.
not pacmandu -sh /var/cache/pacman/pkg - Delete what is no longer installed
This is the safe half: what stays is the installed version of a package still on the machine, so reinstalling it offline still works, while older files for it go. The same run also drops the sync databases of repositories that are no longer configured, which cost nothing to fetch again.
cautionsudo pacman -Sc - Empty it completely, only if you accept the cost
The doubled c removes every file, including those for installed packages. After this a reinstall or a step back to an older version has to download again.
cautionsudo pacman -Scc
What the cache is
The package cache is the directory pacman keeps downloaded package files in, at /var/cache/pacman/pkg, so that the same file does not have to be fetched twice.
Nothing in it is needed for the system to run. It is needed for two things that only matter at the moment you need them: reinstalling a package without a network, and installing an older version again because a new one misbehaves. The files in the cache are the only copy of those older versions you have.
-Sc and -Scc
Against a worked cache holding 6 files for three packages, two of which are still installed:
| Command | Deletes | Keeps | Danger |
|---|---|---|---|
sudo pacman -Sc | The cached files of packages that are no longer installed, and the sync databases of repositories that are no longer configured | The file for the version each package is installed at | caution |
sudo pacman -Scc | Every file in the cache | Nothing | caution |
-Scc also removes the file for the version you are running right now. If a package turns out to be broken after an upgrade, the copy you would have reinstalled is the one this command deleted.
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.
Keeping the last few versions instead
Neither command takes a number of versions to keep, and -Sc is
not the gentle one of the pair for a package you still have: it leaves one file
for that package, the installed version. A program outside pacman takes the
number: the pacman-contrib package ships paccache,
whose -k says how many recent versions of a package stay, so
paccache -rk2 leaves two of them where -Sc leaves one.
The paccache page puts the three side by side on the
same worked cache. It is a separate program with its own options, so check its
own help before running it:
paccache --helpNot part of pacman; it comes from the pacman-contrib package.
Common questions
How do I clear the pacman cache?
Run sudo pacman -Sc. It deletes the cached files of packages that are no longer installed, and the sync databases of repositories that are no longer configured. The local database records the version a package is installed at, so for a package still on the machine that one file stays and older files for it go.
What is the difference between -Sc and -Scc?
-Sc deletes the cached files of packages that are no longer installed, and the sync databases of repositories that are no longer configured. -Scc deletes every file in the cache, including the ones for packages you are currently running.
Where is the pacman cache?
At /var/cache/pacman/pkg. It is an ordinary directory of package files, so its size can be measured with du before you decide to clear it.