Your Search Bar For Shrewd Tips

How Do I Clear Yum Cache In Linux?


Yum keeps local copies of repository metadata and downloaded RPM packages so it can find and install software more efficiently. Over time, those files can take up disk space or become outdated, leading to confusing package-management errors. If you are wondering how to clear the Yum cache in Linux, the usual solution is the yum clean command. It removes cached data without uninstalling software already on your system.

This guide explains the safest ways to clear all or part of the Yum cache, refresh repository information, check how much space the cache uses, and handle systems that use DNF instead of classic Yum.

What Is the Yum Cache?

Yum is a command-line package manager used on Linux distributions such as CentOS, older versions of Red Hat Enterprise Linux (RHEL), and related systems. To install or update software, Yum communicates with configured software repositories. It downloads repository metadata, which describes available packages, and may also download RPM files before installing them.

Yum stores this information locally, commonly under /var/cache/yum/. The cache can include repository metadata, package headers, database files, and downloaded RPM packages. Keeping these files can make repeated operations faster, but the cache may grow or contain metadata that no longer reflects the current state of a repository.

Clearing the cache is generally safe: it does not remove installed applications, system libraries, or user files. Yum may need to download fresh metadata or packages the next time you use it, so the next operation can take longer and use network data.

How to Clear the Entire Yum Cache

To remove all cached data managed by Yum, run:

sudo yum clean all

On a system where you are already logged in as the root user, you can omit sudo:

yum clean all

The command cleans the common categories of cached Yum data, including downloaded packages and repository metadata. The exact details can vary slightly with the Yum version and enabled plugins, but clean all is the standard choice when you want a broad cleanup. Yum will recreate the information it needs the next time it checks repositories or installs software.

You might be prompted to confirm an action, depending on your system’s configuration. Read the message, then confirm if you want to proceed. If the command reports that there was nothing to remove, the cache may already be empty or may have been cleaned previously.

Clear Only Downloaded RPM Packages

If your main goal is to recover disk space used by downloaded package files, remove the cached packages without clearing all repository metadata:

sudo yum clean packages

This is useful when RPM downloads are taking up space but you want to retain other cached data that could speed up later package operations. Clearing packages does not uninstall programs. It only deletes cached installers, which Yum can download again if they are needed later.

Before clearing package files on a machine with limited or unreliable internet access, consider whether you rely on cached RPMs for installation or recovery. Once the cached files are removed, you may need access to the relevant repository to download them again.

Clear Repository Metadata Only

Repository metadata tells Yum which packages and versions are available. If the metadata appears stale, inconsistent, or out of date, clear it with:

sudo yum clean metadata

Yum will fetch fresh metadata the next time it needs repository information. This can help when a repository has changed recently or a package search is not showing the latest available version. It does not, by itself, remove every type of cached data. Use yum clean all if you want to clear the broader cache.

Older Yum releases also support cleaning other cache categories individually, such as headers or database cache. For example:

  • sudo yum clean headers removes cached package headers.
  • sudo yum clean dbcache removes cached repository database files.
  • sudo yum clean expire-cache marks repository metadata as expired so Yum can refresh it when needed.

These narrower commands can be helpful for a particular troubleshooting task, but most users only need yum clean all, yum clean packages, or yum clean metadata.

Refresh Yum Metadata After Cleaning

After clearing metadata, you can ask Yum to build or download the cache for enabled repositories before running an installation. Use:

sudo yum makecache

This command contacts configured repositories and creates local metadata cache files. It can be useful when you want to confirm that repositories are reachable or prepare a system for package operations. Depending on the Yum version and repository configuration, it may download metadata without downloading any RPM packages.

For a simple update, you can usually let Yum refresh metadata as part of the operation instead:

sudo yum update

Use the command that matches your goal. Cleaning and then running makecache is not necessary every time you clear the cache; Yum can refresh the information automatically when it needs it.

Check How Much Space the Yum Cache Uses

To check the approximate size of the usual Yum cache directory, run:

sudo du -sh /var/cache/yum

The -s option displays a total rather than listing every subdirectory, and -h prints the size in a readable format such as megabytes or gigabytes. If the directory does not exist, your distribution may use a different cache location, or the cache may not have been created yet.

You can also inspect the directory contents with:

sudo ls -lah /var/cache/yum

Directory layouts vary by Yum version, operating system, architecture, and repository. Prefer Yum’s own cleanup commands rather than deleting individual cache files by hand. The package manager knows which files it manages and can rebuild its cache safely.

Does Clearing the Yum Cache Remove Installed Packages?

No. The Yum cache contains temporary or reusable data, not the installed software itself. Running sudo yum clean all does not remove installed applications, change user documents, or roll back system updates. It simply removes cached files that Yum can retrieve or recreate later.

To remove an installed package, you would use a package-removal command such as sudo yum remove package-name. That is a different operation and may also remove dependencies, so review the proposed transaction before confirming it.

Likewise, clearing the cache does not repair every package-management problem. If a package database is damaged or a repository is misconfigured, additional troubleshooting may be required. Cache cleanup is most useful when stale metadata or unwanted downloaded files are the issue.

Yum Cache on Newer Linux Systems Using DNF

Many newer Linux distributions use DNF, the successor to Yum. On some systems, the yum command is provided as a compatibility interface to DNF, while other systems expect you to use dnf directly. To clear the DNF cache, run:

sudo dnf clean all

You can also clean only downloaded packages or repository metadata:

  • sudo dnf clean packages removes cached RPM packages.
  • sudo dnf clean metadata removes cached repository metadata.
  • sudo dnf makecache downloads or rebuilds repository metadata.

Check which package manager your system provides with yum --version or dnf --version. If the yum command is unavailable, use DNF commands. On a modern system, cleaning with DNF is generally preferable because it matches the package manager’s current configuration.

Keep Yum Cache From Growing Again

Yum may download packages during future installations and updates, so clearing the cache is not necessarily permanent. If you want to control whether downloaded RPMs remain after a successful installation, check the keepcache setting in Yum’s configuration, typically found in /etc/yum.conf.

The setting is commonly written as:

keepcache=0

With this setting, Yum does not retain package files after a successful transaction. Setting keepcache=1 tells Yum to keep downloaded packages instead. The precise behavior can depend on the Yum version and configuration, and this setting does not replace cleaning files that are already present. Review your organization’s configuration before changing it, particularly on servers that use cached packages for offline maintenance.

Repository metadata is controlled separately, and Yum can recreate it as needed. Avoid changing cache settings simply to fix a one-time issue. In most cases, occasional cleanup with yum clean all is easier and safer.

Common Yum Cache Cleanup Problems

If a cleanup command fails or does not seem to free space, consider these checks:

  • Permission denied: Run the command with sudo, or execute it as root.
  • Command not found: Your distribution may use DNF. Try sudo dnf clean all.
  • Cache still looks large: Check the directory size again and confirm that the files are in Yum’s cache directory. Other tools may have separate package caches.
  • Repository errors after cleanup: Confirm that the system has network access and that repository URLs and credentials are valid. Cleaning the cache cannot fix an unreachable repository.
  • Another package operation is running: Wait for it to finish before starting a cleanup. Do not manually remove cache files while Yum or DNF is actively using them.

If you still see an error, read the full output rather than repeating cleanup commands. The message may point to a repository configuration problem, a permission issue, or a network failure rather than a cache problem.

Conclusion

To clear the Yum cache in Linux, run sudo yum clean all. Use sudo yum clean packages to remove downloaded RPMs only, or sudo yum clean metadata to refresh repository information. These commands do not uninstall software, but Yum may need to download data again during its next operation. On systems that use DNF, run the equivalent sudo dnf clean all. Prefer the package manager’s cleanup commands over manually deleting cache files, and use yum makecache or dnf makecache when you want to refresh repository metadata in advance.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →