Home Arch Linux The Recommended Way To Clean The Package Cache In Arch Linux

The Recommended Way To Clean The Package Cache In Arch Linux

By sk
Published: Last Updated on 233.7K views

This guide explains what is Paccache script and how to clean the package cache in Arch Linux and its variants such as EndeavourOS, and Manjaro Linux.

What is Paccache?

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/ directory.

We also know that Pacman will not delete old or uninstalled packages automatically from the cache. After a particular period of time, the cache folder will grow bigger in size.

So, it is recommended to clean the package cache periodically in Arch Linux 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 cached 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 old packages, you have no choice to install them from the Cache folder. You can only install them from the Arch Linux official repositories.

This is where the Paccache script comes in handy. The Paccache script is provided by the Pacman package itself. So, you don't have to bother with installation steps.

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.

A. Clean the package cache in Arch Linux using Paccache

1. First, check first how many cached packages are available in my cache folder.

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

2. 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 all of them.

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

4. Let me check again 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.

5. Now, check the total disk space used by the cache folder.

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

Great! I have saved up to 4GB disk space.

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

7. To remove all cached versions of uninstalled packages, re-run paccache with u flag:

$ sudo paccache -ruk0

Where, u flag indicates the uninstalled packages.

8. You can also use the following pacman command to remove all uninstalled packages:

$ sudo pacman -Sc

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

Recommended Download - Free eBook: "Learn Linux in 5 Days"

B. Automatically clean package cache in Arch Linux

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

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

Conclusion

In this guide, we learned how to clean the package cache using Paccache script and how to automatically clean the package cache with a pacman hook in Arch Linux. Both these tools will help you keep your Arch Linux clean and save you a lot of disk space.

You May Also Like

17 comments

Dave December 10, 2020 - 6:52 am

Very helpful, thanks.

Reply
Archie February 7, 2022 - 9:14 am

sudo ls /var/cache/pacman/pkg/ | wc -l
0
Thank you very much 🙂

Reply
max October 1, 2022 - 2:01 am

can’t believe we have to do this manually! I had more than 30Gb of cache wtf! Thank you so much for this guide!

Reply
Kanehekili December 22, 2022 - 4:40 am

Very well explained. Thank you!

Reply
1 2

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More