[2018-05-07] Clean pacman package cache automatically in archlinux

To clean up the pacman package cache once you can run:

# paccache -rk2

This will remove all cached packages but keep the 2 versions of any installed one. The arch docu has more useful information.

# mkdir -p /etc/pacman.d/hooks/
# vim /etc/pacman.d/hooks/remove_old_cache.hook

and fill in the following

[Trigger]
Operation = Remove
Operation = Install
Operation = Upgrade
Type = Package
Target = *

[Action]
Description = Keep the last cache and the currently installed.
When = PostTransaction
Exec = /usr/bin/paccache -rvk2

I read about this solution in the arch formus

When you update your system you can see that the hook is running. Your output should be similar to this:

# pacman -Syu
...
( 8/13) Keep the last cache and the currently installed.
removed '/var/cache/pacman/pkg/lib32-glibc-2.26-11-x86_64.pkg.tar.xz'
removed '/var/cache/pacman/pkg/linux-headers-4.16.5-1-x86_64.pkg.tar.xz'
removed '/var/cache/pacman/pkg/glibc-2.26-11-x86_64.pkg.tar.xz'
removed '/var/cache/pacman/pkg/gcc-7.3.1+20180312-2-x86_64.pkg.tar.xz'