Last active
          March 8, 2025 19:37 
        
      - 
      
 - 
        
Save luukvbaal/2c697b5e068471ee989bff8a56507142 to your computer and use it in GitHub Desktop.  
    yay paccache hooks
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | [Trigger] | |
| Operation = Remove | |
| Type = Package | |
| Target = * | |
| [Action] | |
| Description = Clearing cache... | |
| When = PostTransaction | |
| Exec = /home/<user>/.local/bin/tools/removehook | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | [Trigger] | |
| Operation = Upgrade | |
| Type = Package | |
| Target = * | |
| [Action] | |
| Description = Clearing cache... | |
| When = PostTransaction | |
| Exec = /home/<user>/.local/bin/tools/upgradehook | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/bin/sh | |
| yaycache="$(find "$HOME"/.cache/yay -maxdepth 1 -type d | awk '{ print "-c " $1 }' | tail -n +2)" | |
| yayremoved=$(/usr/bin/paccache -ruvk0 $yaycache | sed '/\.cache\/yay/!d' | cut -d \' -f2 | rev | cut -d / -f2- | rev) | |
| [ -z $yayremoved ] || echo "==> Remove all uninstalled package folders" && | |
| echo $yayremoved | xargs -rt rm -r | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/bin/sh | |
| yaycache="$(find "$HOME"/.cache/yay -maxdepth 1 -type d | awk '{ print "-c " $1 }' | tail -n +2)" | |
| echo "==> Keep last 2 installed versions" | |
| /usr/bin/paccache -rvk2 -c /var/cache/pacman/pkg $yaycache | 
This is what I've been using:
[Trigger]
Operation = Upgrade
Operation = Remove
Type = Package
Target = *
[Action]
Description = Clearing cache...
When = PostTransaction
Exec = /usr/bin/bash -c 'yay=$(find /home/$SUDO_USER/.cache/yay -mindepth 1 -maxdepth 1 -type d -exec echo -c {} \;); paccache -rk2 -c /var/cache/pacman/pkg $yay; paccache -ruk0 $yay; find /home/$SUDO_USER/.cache/yay -mindepth 1 -maxdepth 1 -type d -exec bash -c "compgen -G {}/*.pkg.tar.zst > /dev/null || rm -r {}" \;'Edit: Actually now that I'm thinking about it, parsing paccache -ru isn't reliable because it will remove e.g. /home/user/.cache/yay/mutter-performance/mutter-performance-docs-1:44.3-2-x86_64.pkg.tar.zst (because mutter-performance-docs is not installed) thus removing /home/user/.cache/yay/mutter-performance even though mutter-performance is installed.
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
For the scripts, sure. The hook files however are run as root so that's not going to work.