1. Overview

DNF and YUM are package managers used in RHEL-based Linux distributions. Specifically, they’re used to install, update, and remove packages on the Linux machine.

Both DNF and YUM make use of caches that store metadata on downloaded packages. Consequently, these caches allow quicker installations and updates. However, over time, these caches can accumulate and consume space on the Linux machine.

In this tutorial, we’ll learn how to clear both DNF and YUM cache.

All instructions below have been tested on CentOS Stream 9.

2. Clearing YUM Cache

YUM maintains a cache of downloaded package files and metadata, located in the /var/cache/yum directory by default.

In this section, we’ll learn different ways to clean the YUM cache.

2.1. Clean Cached YUM Packages

Packages installed on the Linux machine by the YUM package manager have a local repository cache. Let’s remove the cache:

$ sudo yum clean packages
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
7 package files removed

Therefore, the yum clean command removes any cached packages on the Linux machine and, as a result, frees up disk space.

2.2. Clean Package Headers

Package headers are XML files containing information about packages.

Additionally, YUM utilizes package headers to compare information about available packages with installed ones.

Let’s delete the package headers:

$ sudo yum clean headers
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
7 header files removed

Hence, this command clears the disk space and removes outdated or unnecessary package headers on the Linux machine.

2.3. Clean Metadata for Enabled Repositories

Metadata includes information about available packages. Furthermore, this information consists of version, description, dependencies, architecture, and repository information.

Let’s delete the metadata for enabled repositories:

$ sudo yum clean metadata
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
10 metadata files removed
6 sqlite files removed
16 metadata files removed

As seen in the example above, this command deletes all the metadata on packages and frees up disk space on the Linux machine.

2.4. Clean All YUM Cached Files

With the YUM package manager, we can remove all repository cache at once. To illustrate, let’s clear all cached files in a single command:

$ sudo yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
Cleaning up list of fastest mirrors

As seen above, the all argument instructs YUM to clean all cached files including packages and metadata. Thus, this frees up storage on the Linux machine.

3. Clearing DNF Cache

The DNF package manager, like YUM, retains a cache of downloaded package files and metadata in the /var/cache/dnf directory by default.

In this section, we’ll learn different ways to remove the DNF cache.

3.1. Clean Cached Database Files

Packages have database caches that store information such as file lists or group information.

Let’s remove these database caches used by the DNF package manager:

$ sudo dnf clean dbcache
6 files removed

This command cleans all database caches used by the DNF package manager. Hence, it frees up disk space on the Linux machine.

3.2. Clean DNF Metadata

The DNF package manager stores metadata caches for enabled repositories on the Linux machine.

In the example below, we remove the cached metadata in one command:

$ sudo dnf clean metadata
15 files removed

This command discards the cached metadata files and the next time we run a DNF operation such as installing or updating packages, it fetches the latest metadata from the repositories.

3.3. Clean Cached DNF Packages

The DNF package manager doesn’t automatically remove cache entries when uninstalling packages.

Let’s remove the unwanted cache left out by the package manager:

$ sudo dnf clean packages
6 files removed

As seen in the example above, all storage space used by unwanted cache on the Linux machine has been recovered.

3.4. Clean All DNF Cached Files

All DNF cache stored on the Linux can be removed all at once. To demonstrate, let’s remove all the stored cache in one command:

$ sudo dnf clean all
20 files removed

In the above example, all cached package and metadata files are deleted at once. As a result, disk space is freed up on the Linux machine.

4. Conclusion

In this article, we’ve learned how both DNF and YUM, popular package managers in Red-Hat-based Linux distributions, utilize caches to store downloaded metadata and package information.

Furthermore, these caches offer significant benefits, such as faster installations and updates. However, they also consume storage space on the Linux machine.

Additionally, we discussed different ways of clearing both the DNF and YUM cache using various commands to clear specific parts of the cache or everything at once.

By understanding the discussed points, we can take advantage of the performance benefits while keeping our machines efficient and optimized.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.