The Yellowdog Updater, Modified (YUM) is the primary package management tool for RPM-based Linux distributions like CentOS, RHEL, and Fedora. While YUM automates updates and dependency management effectively, it stores downloaded packages and metadata over time. If left unchecked, this cache can consume significant disk space or cause errors during updates due to corrupted data.
In this guide, you will learn how to safely clear the YUM cache to reclaim disk space and resolve repository issues.
Why clear the YUM cache?
By default, YUM stores downloaded packages and repository data in /var/cache/yum/. While this speeds up future operations, there are several reasons you might need to clear it:
- Reclaim Disk Space: The cache can grow to several gigabytes over time, potentially filling up your
/varpartition. - Fix Metadata Errors: Repository metadata can become corrupted, causing errors like “metadata file does not match checksum”.
- Force Updates: Clearing the cache ensures YUM downloads the absolute latest package information and headers from the remote repositories on the next run.
- Resolve low disk space errors: Particularly useful when
/varor root filesystem approaches capacity.
Important: Clearing YUM cache removes only cached files—it does not affect installed packages or system functionality.
Prerequisites
Before you begin, ensure you have:
- A server running an RPM-based Linux distribution (e.g., CentOS 7, RHEL 7/8).
- Root or
sudoaccess to the command shell. - SSH or direct console access to the server
Where is YUM cache stored?
By default, YUM stores cached data at /var/cache/yum/. This directory contains subdirectories for each enabled repository, storing:
- Downloaded RPM packages (
.rpmfiles) - Repository metadata (XML files describing available packages)
- Package headers (used for dependency resolution)
You can check how much space this directory is currently using by running:
Bash
sudo du -sh /var/cache/yum
Clear specific YUM cache types
You don’t always need to wipe everything. You can choose to remove only specific types of cached data depending on your needs.
Clean cached packages
When you install a package, YUM downloads the RPM file to the cache. These files are not automatically deleted after installation. To remove them without affecting repository data:
Bash
sudo yum clean packages
This frees space from downloaded installers but keeps repository information intact for dependency checking.
Clean headers
Header files contain dependency information used by YUM to resolve software requirements. To remove these old header files:
Bash
sudo yum clean headers
Headers will be re-downloaded automatically when needed for package operations.
Clean metadata
Repository metadata (XML files) allows YUM to know which packages are available. If you are experiencing errors regarding mismatched checksums, clearing the metadata will force YUM to re-download a fresh list of available packages:
Bash
sudo yum clean metadata
Use this when repository information may be outdated or after changing repository configurations.
Clear all YUM cache at once
If you want to free up the maximum amount of space or perform a “hard reset” on your package manager to fix stubborn errors, you can clear packages, headers, and metadata simultaneously.
Bash
sudo yum clean all
This is the most comprehensive cleanup option and the recommended approach when troubleshooting repository issues or maximizing freed space.
Verify cache has been cleared
After running the clean commands, you can verify that the space has been reclaimed by checking the directory size again:
Bash
sudo du -sh /var/cache/yum
You should see a significantly smaller directory size compared to your initial check.
When should you clear YUM cache?
- Low Disk Space: When your
/varpartition or root filesystem is running out of space. - Corrupted Data: If you encounter errors like
metadata file does not match checksum. - Before Major Updates: To ensure you are pulling the freshest dependency tree before a large system upgrade.
- Obsolete Headers: When old headers are causing dependency resolution conflicts.
Note for newer RHEL/CentOS versions
On newer distributions (like RHEL 8/9, CentOS Stream, and Fedora), DNF has replaced YUM as the default package manager. However, yum is typically present as a symbolic link to dnf for backward compatibility.
If you are using a newer system, the commands are functionally identical:
Bash
sudo dnf clean all