User Agents

Web browsers and everyday web tech, explained

Clean Pacman Cache on Arch Linux

· 5 min read · Cookies and Cache

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Cleaning the pacman package cache on Arch Linux frees disk space by removing old downloaded package files (.pkg.tar.zst) that pacman stores in /var/cache/pacman/pkg. This guide shows you how to remove unused versions, keep only the latest, or wipe the cache entirely, plus how to automate the process. You need root privileges (sudo) for all commands.

What You'll Need

  • A working Arch Linux system with pacman
  • sudo access (or root shell)
  • Basic comfort with the terminal

Step-by-Step

  1. Check current cache size. Run du -sh /var/cache/pacman/pkg to see how much space the cache occupies. This gives you a baseline before cleaning.
  2. Remove all cached packages that are no longer installed. Use sudo pacman -Sc. This deletes every .pkg file in the cache that doesn't correspond to a currently installed package. You'll be prompted to confirm; type y and press Enter. This is the safest first step because it keeps all versions of installed packages.
  3. Remove old versions of installed packages (keep only the latest). Run sudo pacman -Scc. This removes all cached packages except the newest version of each currently installed package. It also cleans the sync databases. Confirm with y when prompted. This is the most aggressive built-in clean and is safe for normal use.
  4. Manually delete specific files (optional). If you want to keep certain old versions (for downgrading), list the cache with ls /var/cache/pacman/pkg, then delete individual files with sudo rm /var/cache/pacman/pkg/package-name.pkg.tar.zst. Replace the filename with the actual one.
  5. Wipe the entire cache (optional, but not recommended). Run sudo pacman -Scc twice in a row. The first time removes uninstalled packages, the second removes all remaining files, leaving an empty cache. Only do this if you're certain you won't need to downgrade or reinstall without downloading again.
  6. Automate with a pacman hook (optional). Create a hook file at /etc/pacman.d/hooks/clean-cache.hook with the following content:
    Operation = Install
    Operation = Upgrade
    Type = Path
    Target = /var/cache/pacman/pkg/*.pkg.tar.zst
    [Action]
    Description = Cleaning pacman cache...
    When = PostTransaction
    Exec = /usr/bin/paccache -rk1
    This runs paccache -rk1 after every install/upgrade, keeping only the latest version of each package. The paccache tool is part of the pacman-contrib package; install it with sudo pacman -S pacman-contrib if missing.
  7. Use paccache directly (alternative to -Scc). Install pacman-contrib if not already present. Then run sudo paccache -rk1 to keep the latest version of each package, or sudo paccache -rk2 to keep the latest two versions. Use sudo paccache -ruk0 to remove all uninstalled packages. This gives finer control than -Scc.

Common Mistakes

  • Running -Scc without realizing it removes all old versions. Some users expect it to keep multiple versions; it only keeps the latest. If you need older versions for downgrading, use paccache -rk2 instead.
  • Deleting cache files manually while pacman is running. Never remove files from /var/cache/pacman/pkg during an installation or upgrade. It can cause errors or leave partial files.
  • Assuming -Sc keeps all installed versions. It removes packages that are not installed, but it keeps all versions of installed ones. If you want only the latest, you need -Scc or paccache.
  • Forgetting to confirm the prompt. Both -Sc and -Scc ask for confirmation. If you press Enter without typing 'y', nothing happens. Some users think the command failed.

Tips

  • Check cache size before and after with du -sh /var/cache/pacman/pkg to see how much you freed.
  • Use paccache -rk1 regularly instead of -Scc; it's safer because it keeps the latest version and doesn't touch sync databases.
  • Set up a systemd timer to run paccache weekly: create a service and timer, or simply add the hook as shown above.
  • If you have a slow internet connection, consider keeping at least one old version of critical packages (like the kernel) in case a new update breaks something.
  • Remember that the cache is only for downloaded packages; it does not affect installed software. Cleaning it won't break your system.

Troubleshooting

  • "error: failed to prepare transaction" after cleaning. This usually happens when you try to downgrade a package that was removed from the cache. Re-download the old version from the Arch Linux Archive or use downgrade tool (from AUR) to fetch it.
  • pacman -Scc says "no packages to remove" but the cache is still large. This can happen if there are orphaned files (e.g., .part files from interrupted downloads). Delete them manually with sudo rm /var/cache/pacman/pkg/*.part.
  • Permission denied when running pacman commands. Ensure you're using sudo or are in a root shell. The cache is owned by root.
  • paccache command not found. Install pacman-contrib with sudo pacman -S pacman-contrib.
  • After cleaning, pacman asks to download packages again for a reinstall. That's expected; the cache is only a local copy. If you want to avoid re-downloading, keep more versions with paccache.

Frequently Asked Questions

Is it safe to clean the pacman cache?

Yes, it's safe. The cache only contains downloaded package files; removing them doesn't affect installed software. You might need to re-download a package if you reinstall it later, but that's the only downside.

What's the difference between pacman -Sc and pacman -Scc?

-Sc removes cached packages that are no longer installed. -Scc does that and then removes all old versions of installed packages, keeping only the latest. -Scc is more aggressive.

Will cleaning the cache break my system?

No. The cache is independent of installed packages. Your system will continue to work normally. The only risk is if you need to downgrade a package and the old version is gone.

How often should I clean the cache?

It depends on your disk space and download habits. Many users run paccache weekly via a timer. If you rarely install packages, you can clean less often.

Can I clean the cache without sudo?

No, the cache directory is owned by root. You need root privileges to delete files there.

Does cleaning the cache speed up pacman?

Not directly. Pacman checks the cache for existing packages before downloading, but a large cache doesn't slow it down significantly. The main benefit is disk space.