User Agents

Web browsers and everyday web tech, explained

FreeBSD pkg clean: Clear Package Cache Safely

· 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

pkg clean removes outdated package archives from FreeBSD's local cache, freeing disk space while keeping the most recent versions of installed packages. This guide shows you how to run it safely, what it actually deletes, and how to avoid common pitfalls.

What You'll Need

  • A FreeBSD system with pkg (the binary package manager) installed — it's the default on modern FreeBSD releases.
  • Root or sudo access, because pkg clean modifies system directories.
  • Enough free space in /var/cache/pkg for the cleanup to run (usually trivial, but check with df -h /var/cache/pkg if you're low).
  • An internet connection if you want to re-download any removed packages later — though the cache is only for installation, not runtime.

Step-by-Step

  1. Check current cache usage. Run du -sh /var/cache/pkg to see how much space the cache occupies. This gives you a baseline to compare after cleanup.
  2. List what would be removed (dry run). Use pkg clean -n (or --dry-run) to preview which files will be deleted. This does not change anything — it just prints a list of package archives that are no longer the latest version.
  3. Understand what pkg clean removes. It deletes old versions of packages that are already superseded by newer ones in the cache. For example, if you have foo-1.0.pkg and foo-1.1.pkg, it removes foo-1.0.pkg. It does not remove the latest version of any package, nor does it touch installed packages themselves.
  4. Run the cleanup. Execute pkg clean as root. It will prompt for confirmation unless you add -y (e.g., pkg clean -y) to skip the prompt. The output shows each removed file and the total space freed.
  5. Verify the result. Run du -sh /var/cache/pkg again to confirm space was freed. You can also list the remaining files with ls /var/cache/pkg to see that only the latest versions are kept.
  6. Optional: remove all cached packages. If you want to clear the entire cache (including the latest versions), use pkg clean -a. This is useful if you're short on space and don't plan to reinstall or downgrade soon. Be aware that any future pkg install will re-download packages from the network.

Common Mistakes

  • Using pkg clean -a when you only meant to clean old versions. The -a flag wipes everything, including current versions. If you just want to free space from old archives, omit -a.
  • Assuming pkg clean removes orphaned or unused packages. It does not. It only removes old versions of packages that are still in the cache. To remove unused packages, use pkg autoremove.
  • Running pkg clean without checking the dry-run first. While it's safe, previewing with -n helps you avoid surprises, especially if you have many packages or a custom cache location.
  • Forgetting that the cache is shared across users. If you have multiple jails or users that use pkg, they may rely on the cache. Removing all archives (-a) could slow down their next install.
  • Ignoring the prompt. If you don't use -y, pkg clean asks for confirmation. Accidentally pressing 'y' when you meant 'n' is easy — always read the prompt.

Tips

  • Schedule regular cleanups. Add pkg clean -y to a cron job (e.g., weekly) to keep the cache from growing unbounded. For example, put 0 3 * * 0 root pkg clean -y in /etc/crontab.
  • Combine with pkg update and pkg upgrade. After upgrading packages, run pkg clean to remove the old versions that the upgrade left behind.
  • Use pkg clean -a before creating a system image or snapshot. This reduces the image size if you're cloning the system.
  • Check the manual. Run man pkg-clean to see all options, including --all, --dry-run, and --quiet.
  • Keep the latest version of packages you might downgrade to. If you often need to revert to a previous version, consider not running pkg clean -a — just clean old versions, which is the default.

Troubleshooting

  • Error: "Permission denied" when running pkg clean. You're not root. Use sudo pkg clean or switch to root with su -.
  • Error: "pkg: not found" — pkg is not installed or not in your PATH. On FreeBSD, ensure you've run pkg bootstrap first. If you're using a minimal system, install it via make -C /usr/ports/ports-mgmt/pkg install clean or use the ISO's bootstrap.
  • Disk space not freed after running pkg clean. Check if the cache is on a separate mount point. Run df -h /var/cache/pkg to see if it's full. Also, verify that the files you expected to be removed actually existed — if you already cleaned before, there may be nothing left to remove.
  • Package re-download fails after cleaning. If you removed everything with -a and then try to install a package, you need network access. Check your network configuration and repositories with pkg update.
  • Cache directory doesn't exist. If /var/cache/pkg is missing, pkg clean may fail. Create it with mkdir -p /var/cache/pkg and set proper permissions (usually root:wheel, 0755).
  • Cleanup seems to hang. If it's scanning a huge cache, it can take a moment. Be patient. If it truly hangs, check for NFS or network filesystem issues — the cache might be on a remote mount that's slow.

Frequently Asked Questions

Does pkg clean remove installed packages?

No. It only removes package archives from the cache directory (/var/cache/pkg). Installed packages in /usr/local are untouched.

What's the difference between pkg clean and pkg autoremove?

pkg clean deletes old cached archives. pkg autoremove removes installed packages that are no longer needed as dependencies. They serve different purposes.

How often should I run pkg clean?

It depends on how often you upgrade packages. If you upgrade weekly, run it weekly. If you rarely upgrade, you can run it after each upgrade or monthly.

Can I clean the cache without root?

No. The cache directory is owned by root, and pkg clean needs write access to delete files. You must use sudo or be root.

Will cleaning the cache break my system?

No. The cache is only used during installation. Once packages are installed, they don't rely on the cache files. Removing them is safe.

What does pkg clean -a do exactly?

It removes all package archives in the cache, including the latest versions. After that, any pkg install or pkg upgrade will re-download what it needs.