A year and a half ago i wrote an article about something I liked about Manjaro, by extension also available on other Arch Linux based systems. I am talking about the possibility of downgrading a package to a previous version. In part this is possible because of the packet cache of Arch, in which the last three versions of each package are saved, by default. Sure, this sounds good, but not so good if your hard drive doesn't have a lot of storage.
Until SSDs are as cheap as they've ever been, if that time ever comes, hard drives can be one of three options: big and expensive; small and cheap; or a hybrid, where there is part SSD and part HDD. In the second and third option we can have the same problem, since the HDD, the one that offers more storage, we usually leave it for data and we use the SSD for the operating system. It is in these cases that we need to keep an eye on the Arch Linux package cache. And, if necessary, clean it.
Table of Contents
Clear the Arch package cache, delete it or leave it as it is
This cache will save the last three versions of each package in /var/cache/pacman/pkg. You have to watch it, because even with the limit of three, it can happen that the folder grows and grows until it touches or exceeds 10GB of storage, depending on the amount of software that we have installed. And, remember, we are talking about cache, that is, something that is there just in case, but that may not be necessary.
Pacman has an option to remove old versions from the cache. sudo pacman -Sc will remove only the old versions and it will leave the last one we have installed, that is, the pkg of the one we have installed. Although it is an option of Pacman, it is said that it is not something recommended, but the argument would be that we could not do the downgrade as we are supposed to. In this case we would have to go to the official repositories and choose a previous version.
The one that manages this from the last three versions is paccache, and we can reclaim space with these commands:
The first thing would be to look at how many packages we have installed and see what they occupy, in my case a total of 1981 packages with a total weight of 13GB:
sudo ls /var/cache/pacman/pkg/ | wc -l du -sh /var/cache/pacman/pkg/
If we want to remove all but the last three packages, something that is usually automatic but might not be working, we will write:
sudo paccache -r
If you realize, it has recovered little (1.52GB) for me, and I am not worried about not being able to go back to a previous version, so then I have used the sudo pacman -Sc:
With the latter, out of 13GB that I had in the Arch package cache, Manjaro in my case, I was left with only 4GB. It will ask us two questions: in the first one, it asks us if we want to eliminate the packages, and there we have to say yes; in the second it tells us to remove repositories, and there it is better to say no.
Still want to delete more?
A hard drive has to be very bad or you have to have many hobbies for a deeper cleaning to be necessary, but it can be done. The command would be sudo paccache -rk 1, Where the k indicates the number of packets to keep. If you want to remove all packages already uninstalled, you can do it with the command sudo paccache -ruk0. From the above, the u indicates uninstalled packages.
If you want to delete all the cache, and leave it at 0GB, the command would be sudo pacman -Scc, but this I would recommend only in extreme cases where we no longer have space on the hard drive. Everything deleted with the commands exposed here cannot be recovered.
Automate the process
All this Arch Linux package cache clearing can be done automatically, but by creating a file and adding a few lines to it. The steps to follow to automate this cleaning task would be:
- We open a terminal and write:
sudo mkdir /etc/pacman.d/hooks
- Now we write this other command:
sudo nano /etc/pacman.d/hooks/clean_package_cache.hook
- Inside the editor, we add these lines:
[Trigger] Operation = Upgrade Operation = Install Operation = Remove Type = Package Target = * [Action] Description = Cleaning pacman cache... When = PostTransaction Exec = /usr/bin/paccache -r
- We save and save. From now on, the package cache will be automatically cleaned every time Pacman works updating, installing or removing packages.
And so can you get some extra space. It is clear that if the hard drive is large enough, it is best to leave things as they have been designed, but these options exist and can get us out of trouble.