How to Clear Package Cache in Arch Linux

Arch Linux has become a popular Linux distribution because of its split between usability and control. It offers all the tools a power user needs to maintain complete control over his system, yet it also provides newbies with the helpful instruction guides on how to complete nearly any task.

One thing newbies often forget, though, is that no matter how user-friendly their new Gnome desktop running Firefox and LibreOffice may be during the work day, leftovers from those and other installed packages can fill up disk space over time.

When you upgrade programs with Arch Linux’s package manager Pacman, you need to periodically clear its cache. Otherwise, your hard disk will fill with unwanted and unneeded old packages. This tutorial will show you how.

What Is the Package Cache Anyway?

Arch uses Pacman, like with pacman -S firefox, to install a package from the Arch repositories. In the case of that command, the -S option tells Pacman to look for a compressed tarball (a tar.xz file) named “firefox.” If it finds a newer version than what is installed on the user’s computer, Pacman then offers the option to install the newer package.

This type of operation often happens automatically for multiple packages when you run the full system upgrade command pacman -Syu, using -y to download the latest repository list of available packages, -u to find all out-of-date packages on your computer, and -S to install any new packages Pacman finds.

For any new package Pacman first downloads the tarball into its cache directory – usually at “/var/cache/pacman/pkg/” – and then uses that file to install the new version. The history of previously-installed packages might look something like the following image.

It’s clear from this screenshot that Firefox doesn’t take up a lot of room by itself. Across six packages, it only uses a few hundred Mb of room. However, that same hundred megabytes across several hundred packages can begin to accumulate to undesirable levels.

Upon closer inspection, the entire package cache on this system reads 4.8 GB. It’s time to clear some space.

How to Clear the Cache

The screenshot above also reveals that Pacman doesn’t automatically delete the old tarballs when new packages are installed. It also leaves tarballs alone when you delete packages. Therefore, you must clear the cache manually.

There are multiple ways you can proceed. First, you can use pacman itself:

sudo pacman -Sc

This option will delete all the cached packages in “/var/cache/pacman/pkg/” except for the versions matching packages installed on your system. There are some dangers to this approach which are covered in the following section.

For a safer, more controlled approach, you can use the paccache script, which is provided with any Pacman installation.

sudo paccache -r

removes all cached tarballs except for the most recent three packages. It does this for all packages that are and are not installed on your computer.

You can now see that the script has removed a large portion of this system’s cache. It also left three copies of Firefox in the cache directory.

paccache -rk1

lets you choose the number of packages you want to keep. In this case the choice of “1” emulates much of what pacman -Sc achieves, except that paccache still doesn’t discriminate between installed and uninstalled packages.

paccache -ruk0

gives you control over uninstalled packages by removing the tarballs of all packages not currently installed on your local workstation.

Dangers of Cache Clearing

Although it’s largely a good idea to clear unneeded packages from time to time, you can run into problems if you’re overzealous.

Note first that pacman -Sc will delete the tarballs of packages not currently installed on your computer. Therefore, if you deleted Firefox but wanted to install it at a later date but then told Pacman to clear your cache, you could be out of luck.

Yes, you can just download the latest Firefox when you want to install it again. However, there is no guarantee the newest version will work well on your system, so if you clear your entire package cache, downgrading to a previous Firefox version will not be possible without using an online package archive.

It should also be noted that you can clear your entire cache directory with the pacman -Scc command. This would force you to reinstall any packages from a fresh Internet download, so be careful when taking this route. Generally, it isn’t necessary to clear your whole cache unless you need the disk space.

Conclusion

Arch gives you a lot of freedom to shape your system as you see fit. Pacman makes it easy to install and remove packages, but it also requires some manual intervention at regular intervals.

Take a quick look at your own package cache. If it’s getting a bit full, make it a point today to clear away some of the excess.