Skip to content

Instantly share code, notes, and snippets.

@lfom
Created November 12, 2022 19:43
Show Gist options
  • Save lfom/a7e0b79389e05644ce6a40129f531cdb to your computer and use it in GitHub Desktop.
Save lfom/a7e0b79389e05644ce6a40129f531cdb to your computer and use it in GitHub Desktop.
Clear paru/pacman cache
#!/usr/bin/env bash
echo "** Starting cleaning pacman/AUR cache..."
# pacman cache directory
pcachedir="$(awk '/Cache/ {print $3}' /etc/pacman.conf)"
# paru/AUR cache directory
acachedir="$HOME/.cache/paru/clone"
toremove="$(comm -23 <(basename -a $(find $acachedir -mindepth 1 -maxdepth 1 -type d) | sort) <(pacman -Qqm) | xargs -r printf "$acachedir/%s\n")"
echo "** Removing cache for foreign packages that are not installed anymore"
for deldir in $toremove; do
echo "* $deldir"
rm -rf "$deldir"
done
pkgcache="$(find $acachedir -mindepth 1 -maxdepth 1 -type d | xargs -r printf "%s\n")"
echo "** Removing untracked/source files for AUR packages"
for pkgdir in "$acachedir"/*/; do
pkgname=$(basename "$pkgdir")
echo "* $pkgname"
## Remove untracked files (e. g. source/build files) excepting package files and main source files for installed version if non-git package
if [[ ! "$pkgname" =~ ^.*-git$ ]]; then
pkgver="$(pacman -Q $pkgname 2>/dev/null | cut -d ' ' -f2 | cut -d '-' -f1 | cut -d ':' -f2)"
cd "$pkgdir"
toremove="$(git ls-files --others | grep -v -e '^.*\.pkg\.tar.*$' -e '^.*/$' -e "^.*$pkgver.*$" | xargs -r printf "$pkgdir/%s\n")"
[ -z "$toremove" ] || rm -f $toremove
fi
[ -d "${pkgdir}/src" ] && echo "** removing: ${PWD}/${pkgdir}/src" && rm -rf ${pkgdir}/src/ || true
done
## Remove everything for uninstalled foreign packages, keep latest version for uninstalled native packages, keep two latest versions for installed packages
echo "** Removing cached AUR packages (keep two latest version for installed ones)"
for deldir in $pkgcache; do
echo "* $deldir"
/usr/bin/paccache -qrk2 -c "$deldir"
done
echo "** Removing cached pacman packages (keep latest version of uninstalled package, latest two otherwise)"
/usr/bin/paccache -qruk1 -c "$pcachedir"
/usr/bin/paccache -qrk2 -c "$pcachedir"
echo "** Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment