Skip to content

Arch Linux Pacman

Clearing Package Cache

Packages downloaded by Pacman are stored in "/var/cache/pacman/pkg". The downloaded packages are not cleared over time even when a package is uninstalled.

With the use of "paccache" script provided by the "pacman-contrib" package, it allows for deletion of all cached versions of installed and uninstalled packages. By default the most recent 3 cached versions will be kept.

Solution: Manual

  1. First install the required package.
    # pacman -Sy pacman-contrib
    
  2. Remove cached packages.

    Remove all except the most 3 recent cached versions.

    # paccache -r
    
    Remove all except the most recent 2 cache versions.
    # paccache -rk2
    

  3. Remove all cached versions for uninstalled packages.

    # paccache -ruk0
    

Note

A test run of the command paccache can be done by adding the parameter -d (e.g. paccache -dr). This would be a dry run, only displaying possible packages that could be removed.

Solution: Hook

The hook will be ran automatically each time a package is installed, removed or upgraded and removes all packages except the most 3 recent cached versions.

  1. Install dependency.
    # pacman -Sy pacman-contrib
    
  2. Create directory.
    # mkdir -p /etc/pacman.d/hooks
    
  3. Create hook.
    # nano /etc/pacman.d/hooks/pacman-cache.hook
    
    [Trigger]
    Type = Package
    Operation = Remove
    Operation = Install
    Operation = Upgrade
    Target = *
    
    [Action]
    Description = Remove unused package files cached by pacman in user's home directory
    Depends = bash
    Depends = pacman-contrib
    When = PostTransaction
    Exec = /usr/bin/bash -c '/usr/bin/paccache -r;'
    

Solution: Systemd Timer

Default behaviour is to run weekly to clear unused packages and keep the 3 most recent cached versions.

  1. Install required package.
    # pacman -Sy pacman-contrib
    
  2. Edit timer file as desired (optional).
    # systemctl edit paccache.timer
    
    ### Editing /etc/systemd/system/paccache.timer.d/override.conf
    ### Anything between here and the comment below will become the new contents of the file
    
    OnCalendar=weekly
    
    ### Lines below this comment will be discarded
    
    ### /etc/systemd/system/paccache.timer
    # [Unit]
    # Description=Discard unused packages weekly
    #
    # [Timer]
    # OnCalendar=weekly
    # AccuracySec=1h
    # Persistent=true
    #
    # [Install]
    # WantedBy=timers.target
    
  3. Edit service unit file (optional).
    # systemctl edit paccache.service
    
    ### Editing /etc/systemd/system/paccache.service.d/override.conf
    ### Anything between here and the comment below will become the new contents of the file
    
    ExecStart=/usr/bin/paccache -rk2
    
    ### Lines below this comment will be discarded
    
    ### /etc/systemd/system/paccache.service
    # [Unit]
    # Description=Discard unused packages
    #
    # [Service]
    # Type=oneshot
    # ExecStart=/usr/bin/paccache -r
    
  4. Enable paccache timer.
    # systemctl enable paccache.timer