The Recommended Way To Clean The Package Cache In Arch Linux

Clean The Package Cache In Arch Linux

We all know that Pacman, the default package manager for Arch Linux and its derivatives, will store all downloaded packages in /var/cache/pacman/pkg/ folder. And, we also know that it will not delete old or uninstalled packages automatically. After a particular period of time, the cache folder will grow bigger in size. So, it is recommended to clean the cache folder periodically to free up the hard disk’s space. Pacman has a built-in option to remove all cached packages. You can clean the cached packages by running “sudo pacman -Sc” command. However, this command will remove all old versions and leave only the versions of packages which are currently installed available. This is not a recommended way. Because, sometimes you might want to downgrade a particular package to its older version. So, if you cleaned all the old package versions, you have no choice to install them from the Cache folder. hence, you can only install them from the Arch Linux official repositories. This is where the Paccache script comes in helps.

The Paccache script is provided by the Pacman package itself. So, you don’t have to bother with installation steps. Unlike the pacman, Paccache will keep the 3 most recent package versions by default. Except the 3 most recent package versions, It will delete all cached versions of each package regardless of whether they’re installed or not. This brief tutorial teaches how to properly clean the package cache in Arch Linux and its derivatives using paccache script. Read on.

The Recommended Way To Clean The Package Cache In Arch Linux

Let me check first how many cached packages are available in my cache folder.

$ sudo ls /var/cache/pacman/pkg/ | wc -l
3185

As you see in the above output, I have totally 3185 cached packages. Let me check the total disk space used by the cache folder.

$ du -sh /var/cache/pacman/pkg/
9.7G /var/cache/pacman/pkg/

Currently, I have cached packages of 9.7 GB in size. This is too much. I don’t want to keep to all of them.

To clean all packages, except the 3 most recent versions, run the following command:

$ sudo paccache -r
[sudo] password for sk:

==> finished: 854 packages removed (disk space saved: 4.37 GiB)

See? Paccache removed 854 old and/or uninstalled packages from the cache and saved 4.37 GB disk space. Let me check how many packages are left in the cache folder.

$ sudo ls /var/cache/pacman/pkg/ | wc -l
2331

Great! Now, there are 2331 cached packages available in the Cache folder. These are the 3 most recent versions of each cached package.

Let me again check the total disk space used by the cache folder.

$ du -sh /var/cache/pacman/pkg/
5.4G /var/cache/pacman/pkg/

Now, I have saved up to 4GB disk space.

Still want to remove more packages? Of course, you can! Paccache allows you to decide how many recent versions you want to keep. For instance, run the following command if you want to keep only one most recent version:

sudo paccache -rk 1

Where, k indicates to keep “num” of each package in the cache.

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

sudo paccache -ruk0

Where, u flag indicates the uninstalled packages.

Or, simply use the following pacman command to remove all uninstalled packages:

sudo pacman -Sc

To completely remove all packages (Whether they are installed or uninstalled) from the cache, run the following command:

sudo pacman -Scc

Please be careful while using this command. There is no way to retrieve the cached packages once they are deleted.

Automatically clean the package cache

If you are too lazy to clean the package cache manually, you can automate this task using pacman hooks. The pacman hook will automatically clean the package cache after every pacman transaction.

To do so, create a file /etc/pacman.d/hooks/clean_package_cache.hook:

sudo mkdir /etc/pacman.d/hooks
sudo nano /etc/pacman.d/hooks/clean_package_cache.hook

Add the following lines:

[Trigger]
Operation = Upgrade
Operation = Install
Operation = Remove
Type = Package
Target = *
[Action]
Description = Cleaning pacman cache...
When = PostTransaction
Exec = /usr/bin/paccache -r

Save and close the file. From now on, the package cache will be cleaned automatically after every pacman transactions (like upgrade, install, remove). You don’t have to run paccache command manually every time.

For more details, refer the Paccache help section by running the following command:

paccache -h

And, that’s all for now. Hope this helps. You know now how to properly remove the package cache in Arch Linux and derivatives. We will be posting more useful guides in the day to come. If you find our guides useful, please take a moment to share them on your social, professional networks, so everyone can benefit from them.

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...

  • John Piers Cilliers

    Nice and simple! Cool tutorial for the beginner ARCH Linux user and also a nice reminder for those of us that have been using ARCH for a while.

  • I’m root

    thanks!

  • Working for me, thanks!