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

The Recommended Way To Clean The Package Cache In Arch Linux

By sk
19644 Views

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. We also know that it 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 In Arch Linux 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 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 helps.

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.

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

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

11 comments

John Piers Cilliers August 10, 2017 - 2:00 am

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.

Reply
I'm root October 7, 2017 - 6:53 pm

thanks!

Reply
FDFRS November 3, 2017 - 10:30 am

Working for me, thanks!

Reply
Martin November 12, 2018 - 3:45 pm

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

I guess ALA is a myth then.

archive.archlinux.org

Reply
CHOPPERGIRL December 15, 2018 - 11:50 am

Why are the package files kept at all. I want them *all* deleted after I install some software. They are just wasting space on my SSD drive and I have no idea why the fuck they would be kept after the installation process is through. What is the command to do that, erase all packages?

Reply
Mubashir February 17, 2019 - 10:07 pm

In the hook example above, replace “paccache -r” with “paccache -rk0”. Note that this is not recommended, see arch wiki for system maintenance.

Reply
Juan January 27, 2019 - 8:17 pm

Gostosa! I mean… thanks!

Reply
chalist September 20, 2019 - 8:47 pm

Great. Thank you

Reply
RomanK September 30, 2019 - 4:41 am

thank you! you saved me from this routine 🙂

Reply
thank you January 3, 2020 - 4:11 am

Thank you so much!

Reply
Pedro April 23, 2020 - 5:55 am

Very complete and clear article. Thanks a lot for sharing

Reply

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. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More