How to Remove UnWanted Cached Packages on Arch Linux

In this guide on How to Remove Cached Packages on Arch not needed using Pacman, I'll show you the steps taken to remove orphaned packages that are usually cached on Arch.  The Pacman package manager is one of the major distinguishing features of Arch Linux, it has the power to combine a simple binary package and package build system similar to Gentoo. Pacman makes it easy to manage both official Arch upstream packages and other users' build packages.

As you continue downloading and installing packages on Arch with Pacman, there are usually left-overs which tend to consume more disk space as the data grows. It is beneficial when you have the slow bandwidth and you keep re-installing packages. But if you have good bandwidth and would like to do the cleaning of cached packages, you can use this guide for reference.

Read also : How to List Installed Packages by Date on Linux

Remove Cached Packages on Arch

The storage directory for pacman downloaded packages is /var/cache/pacman/pkg/.  The old and uninstalled packages are not automatically removed and you need to deliberately do the cleaning manually. You can choose to clean all cached packages or specific.

Remove all the cached packages that are not currently installed

To remove all packages in the Cache that are not currently installed on your system, use the command:

$ sudo pacman -Sc
Remove Cached Packages on Arch

Explanation of options used is as below:

-c, --cascade: Remove all target packages, as well as all packages that depend on one or more target packages. This operation is recursive and must be used with care since it can remove many potentially needed packages.

-S, --sync : Synchronize packages

It is recommended that you run this command when certain that previous package versions are not required anymore e.g for later downgrades. The version of all packages currently installed on the system will remain untouched.

Remove all the cached packages by emptying the cache folder

It is possible to clear everything inside the /var/cache/pacman/pkg/ directory using below command:

$ sudo pacman -Scc

Cache directory: /var/cache/pacman/pkg/
:: Do you want to remove ALL files from cache? [y/N] y
removing all files from cache...

Database directory: /var/lib/pacman/
:: Do you want to remove unused repositories? [Y/n] y
removing unused sync repositories...

This also prevents from reinstalling a package directly from the cache folder in case of need, thus requiring a new download. It should be avoided unless there is an immediate need for disk space.

Delete all cached versions of each package using paccache

The next command will delete all cached versions of each package regardless of whether they're installed or not, except for the most recent 3, by default:

# sudo paccache -r
==> no candidate packages found for pruning

The operation commands available are:

-d, --dryrun : Perform a dry run, only finding candidate packages.
-m, --move <dir> : Move candidate packages to "dir".
-r, --remove : Remove candidate packages.

Options that can be used with operation commands above include:

-f, --force : apply force to mv(1) and rm(1) operations.
-i, --ignore <pkgs>  : ignore "pkgs", comma-separated.
-k, --keep <num> : keep "num" of each package in the cache (default: 3).
-q, --quiet : minimize output
-u, --uninstalled :target uninstalled packages.

For example to keep 10 recent packages, you'll use:

# paccache -rvk 10

To remove all cached versions of uninstalled packages, re-run paccache with:

# paccache -ruk0

Since 0 is used in place of number of packages to keep, all uninstalled packages will be removed.

Clean Cache automatically after every pacman transaction

You can also create pacman hooks that runs automatically after every pacman transaction to clean pacman cache. Pacman pre- and post-transaction hooks are loaded from /usr/share/libalpm/hooks/ directory. Sample post-hook script is shown below:

Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = *

Description = Cleaning pacman cache...
When = PostTransaction
Exec = /usr/bin/paccache -r

Another one

Operation = Remove
Operation = Install
Operation = Upgrade
Type = Package
Target = *

Description = Keep the last cache and the currently installed.
When = PostTransaction
Exec = /usr/bin/paccache -rvk10

Both have been defined as PostTransaction hooks which means they run after every successful pacman transaction. Exec command options used are as shown earlier, so it should be easy for you to write your own.

Leave a Comment