How to Clean Package Cache (`pacman -Sc`) on Arch Linux

How to Clean Package Cache (pacman -Sc) on Arch Linux

Managing disk space and maintaining a clean and efficient system is a priority for any Arch Linux user. One of the simplest but often overlooked ways to do this is by cleaning up the package cache. Arch Linux uses the pacman package manager, which caches downloaded packages in a local directory. Over time, this cache can grow significantly and consume a lot of disk space, especially for users who frequently update their systems.

In this article, we’ll explore how to clean the package cache using the pacman -Sc command, explain what it does, when and why to use it, and cover some alternatives and best practices to help you keep your system tidy and efficient.


Understanding the Package Cache

Before diving into the cleanup process, it’s important to understand what the package cache is and why it exists.

What Is the Package Cache?

When you install or upgrade packages on Arch Linux using pacman, the package manager downloads the required .pkg.tar.zst (or .pkg.tar.xz in older systems) files from the repositories. These files are stored in the package cache directory:

/var/cache/pacman/pkg/

These cached files allow pacman to reinstall or downgrade packages without needing to download them again. This is particularly useful when:

  • Reinstalling a package after removing it accidentally.
  • Downgrading a package if a new version causes issues.
  • Avoiding unnecessary downloads on systems with limited or expensive internet.

However, unless managed, this cache can build up rapidly.


Why Clean the Package Cache?

Cleaning the package cache frees up disk space, which is especially valuable on systems with small root partitions (such as those installed on SSDs or minimal containers). If you’re running low on space and have never cleaned the cache, it’s likely you can reclaim hundreds of megabytes, if not gigabytes, of disk space.

Example Scenario

Imagine a user who updates their system weekly and uses rolling-release packages like Linux kernels, browsers, and office suites. Each update adds a new version to the cache, but old versions remain unless manually removed. Over several months, this could easily amount to several gigabytes of redundant packages.


Cleaning the Cache with pacman -Sc

Now that we know what the package cache is and why it’s important to clean it, let’s explore the command that handles this: pacman -Sc.

The Basic Command

sudo pacman -Sc

This command performs the following actions:

  • Removes all uninstalled package files from the cache.
  • Keeps only the packages that are currently installed.
  • Also removes unused repository databases.

After running this command, pacman will prompt you to confirm the deletion:

Do you want to remove all other packages from cache? [y/N]

You must confirm with y to proceed. Otherwise, the operation is canceled.

Example Output

sudo pacman -Sc

Packages to keep:
  All installed packages
Packages to remove:
  All uninstalled packages

Do you want to remove unused repositories? [Y/n] y
Do you want to remove all other packages from cache? [y/N] y

removing old packages from cache...
removing unused sync repositories...

What Happens Behind the Scenes

When you run pacman -Sc, it performs the following under the hood:

  • Scans the /var/cache/pacman/pkg/ directory.
  • Compares the cached packages with currently installed versions.
  • Deletes any packages that are no longer installed on your system.
  • Optionally removes unused repository databases in /var/lib/pacman/sync/.

This is a safe cleanup option. It preserves the packages for currently installed software, allowing you to reinstall without redownloading them.


Important Considerations and Risks

While pacman -Sc is relatively safe, it’s important to understand its implications.

1. Downgrade Limitation

After cleaning, you won’t be able to downgrade to previous versions of packages without redownloading them from the Arch archive (or a custom mirror), unless you have copies elsewhere.

2. No Recovery for Removed Packages

If you remove a package and then clean the cache, that package’s cached version is gone. You’ll need internet access to reinstall it.

3. Rebuilding System with Cache

Some users prefer to keep a full cache so they can rebuild or restore their system offline using the cached packages. If this applies to you, consider a more conservative cleanup approach.


Alternative: paccache – A Safer Tool

Arch provides an official script called paccache (part of the pacman-contrib package) for more flexible cache management.

Installing pacman-contrib

sudo pacman -S pacman-contrib

Using paccache

The most common usage is:

sudo paccache -r

This command removes all cached versions of packages except the three most recent versions of each. This strikes a balance between saving space and retaining downgrade/reinstall capabilities.

You can customize how many versions to keep:

sudo paccache -rk1

This keeps only one version of each installed package and removes the rest.

To clean uninstalled packages only:

sudo paccache -ruk0

This removes all cached versions of uninstalled packages.


Fully Cleaning the Cache: pacman -Scc

If you want to wipe the cache completely, including currently installed package versions, use:

sudo pacman -Scc

This is a more aggressive command. It does the following:

  • Deletes all cached packages, installed or not.
  • Deletes all repository databases.

You will receive two prompts for confirmation:

Do you want to remove all files from cache? [y/N]
Do you want to remove unused sync repositories? [y/N]

This should only be used if you’re truly desperate for space or if you’re confident you won’t need the cached packages again.

WARNING: Use with caution

If you remove the cache and a package gets corrupted, you will need to redownload it. Also, reinstalling or downgrading without internet access becomes impossible.


Automating Cache Cleanup

If you want to regularly clean the cache without thinking about it, you can set up a systemd timer or cron job.

Using a systemd Timer

  1. Create a systemd service file:
sudo nano /etc/systemd/system/paccache-clean.service

Paste the following:

[Unit]
Description=Clean pacman package cache

[Service]
Type=oneshot
ExecStart=/usr/bin/paccache -r
  1. Create a timer file:
sudo nano /etc/systemd/system/paccache-clean.timer

Paste:

[Unit]
Description=Run paccache weekly

[Timer]
OnCalendar=weekly
Persistent=true

[Install]
WantedBy=timers.target
  1. Enable and start the timer:
sudo systemctl enable --now paccache-clean.timer

This will clean the cache weekly and help keep disk usage under control.


Best Practices

Here are some general tips for managing your package cache:

  • Run pacman -Sc periodically if you want to free up space but still keep current packages.
  • Use paccache for safer, more controlled cache management.
  • Keep at least one previous version of each package if you often troubleshoot updates.
  • Avoid pacman -Scc unless absolutely necessary.
  • Automate with systemd timers if you prefer hands-off maintenance.

Conclusion

Cleaning the package cache on Arch Linux is an essential task that can save valuable disk space and help keep your system lean. The pacman -Sc command provides a safe, moderate way to clear out unneeded package files while retaining those that are still installed. For users needing more control, tools like paccache are excellent alternatives.

Just remember that your package cache is a useful resource, and blindly deleting it can have downsides if you ever need to reinstall or downgrade a package. Choose the right cleanup strategy for your system and workflow, and you’ll enjoy a more efficient and responsive Arch Linux experience.