FreeBSD pkg clean: Clear Package Cache Safely
In this article
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 cleanmodifies system directories. - Enough free space in
/var/cache/pkgfor the cleanup to run (usually trivial, but check withdf -h /var/cache/pkgif 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
- Check current cache usage. Run
du -sh /var/cache/pkgto see how much space the cache occupies. This gives you a baseline to compare after cleanup. - 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. - Understand what
pkg cleanremoves. It deletes old versions of packages that are already superseded by newer ones in the cache. For example, if you havefoo-1.0.pkgandfoo-1.1.pkg, it removesfoo-1.0.pkg. It does not remove the latest version of any package, nor does it touch installed packages themselves. - Run the cleanup. Execute
pkg cleanas 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. - Verify the result. Run
du -sh /var/cache/pkgagain to confirm space was freed. You can also list the remaining files withls /var/cache/pkgto see that only the latest versions are kept. - 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 futurepkg installwill re-download packages from the network.
Common Mistakes
- Using
pkg clean -awhen you only meant to clean old versions. The-aflag wipes everything, including current versions. If you just want to free space from old archives, omit-a. - Assuming
pkg cleanremoves orphaned or unused packages. It does not. It only removes old versions of packages that are still in the cache. To remove unused packages, usepkg autoremove. - Running
pkg cleanwithout checking the dry-run first. While it's safe, previewing with-nhelps 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 cleanasks for confirmation. Accidentally pressing 'y' when you meant 'n' is easy — always read the prompt.
Tips
- Schedule regular cleanups. Add
pkg clean -yto a cron job (e.g., weekly) to keep the cache from growing unbounded. For example, put0 3 * * 0 root pkg clean -yin/etc/crontab. - Combine with
pkg updateandpkg upgrade. After upgrading packages, runpkg cleanto remove the old versions that the upgrade left behind. - Use
pkg clean -abefore creating a system image or snapshot. This reduces the image size if you're cloning the system. - Check the manual. Run
man pkg-cleanto 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. Usesudo pkg cleanor switch to root withsu -. - Error: "pkg: not found" —
pkgis not installed or not in your PATH. On FreeBSD, ensure you've runpkg bootstrapfirst. If you're using a minimal system, install it viamake -C /usr/ports/ports-mgmt/pkg install cleanor use the ISO's bootstrap. - Disk space not freed after running
pkg clean. Check if the cache is on a separate mount point. Rundf -h /var/cache/pkgto 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
-aand then try to install a package, you need network access. Check your network configuration and repositories withpkg update. - Cache directory doesn't exist. If
/var/cache/pkgis missing,pkg cleanmay fail. Create it withmkdir -p /var/cache/pkgand 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.