Your Search Bar For Shrewd Tips

How To Clean Dnf Cache


When package operations fail, downloads become unusually large, or disk space runs low, cleaning the DNF cache can help. DNF stores repository metadata and downloaded package files so it can work more efficiently. Over time, those cached files may take up space or become outdated. This guide explains how to clean the DNF cache safely, which commands to use, and what to expect on Fedora, RHEL, Rocky Linux, AlmaLinux, and other DNF-based systems.

How to Clean the DNF Cache

The quickest way to remove all cached DNF data is to run:

sudo dnf clean all

This removes cached repository metadata and downloaded package files. DNF can download fresh metadata the next time it needs it, so cleaning the cache generally does not uninstall software or change your installed packages. It may, however, make the next update or installation take longer because DNF needs to retrieve information again.

If you only want to clear one type of cached data, use a more specific cleanup command instead. The sections below explain what each option does and when it is useful.

What Is the DNF Cache?

DNF is the package manager used by Fedora and many Red Hat Enterprise Linux-compatible distributions, including Rocky Linux and AlmaLinux. It uses repositories to find, download, install, and update software. To avoid retrieving the same information repeatedly, DNF saves local copies of some repository data and package downloads.

On many systems, DNF cache files are stored under /var/cache/dnf. The exact contents and layout can vary by distribution, DNF version, and configuration. Common cached data includes repository metadata, database files created from that metadata, and package files downloaded for installation or updates.

The cache is useful: it can speed up routine package operations and may let you reuse packages that were already downloaded. But it can grow, contain expired repository information, or become inconsistent after interrupted downloads or repository changes. Cleaning it can reclaim disk space or help resolve certain package-manager problems.

Clean All DNF Cache Files

To remove all DNF cache data, run:

sudo dnf clean all

The sudo command runs DNF with administrator privileges, which are usually required to clean system-wide cache files. Enter your password if prompted. DNF will remove its cached data, but it will not remove installed applications or erase personal files.

Use this option when you want a thorough cleanup, when you have plenty of network access to refresh repository data, or when more targeted cleanup has not resolved a cache issue. Afterward, a package operation such as sudo dnf check-update or sudo dnf install package-name may take longer while DNF downloads current metadata.

Keep in mind that clearing the cache can remove downloaded RPM files that you might otherwise reuse. If you need to install or reinstall packages without internet access, do not remove cached package files until you have confirmed that you have another copy of the required installers.

Clean Only Downloaded Packages

To remove downloaded package files while leaving other cache data in place, use:

sudo dnf clean packages

This is a helpful option when the cache is taking up disk space because of previously downloaded RPMs. It does not uninstall the packages that are already installed on your system. It removes cached downloads, so DNF may need to download those files again if you later install or reinstall the same software.

If you are unsure whether cached packages are using significant space, inspect the cache directory before cleaning it:

sudo du -sh /var/cache/dnf

This reports the directory’s approximate total size. You can also list its contents with sudo ls -lah /var/cache/dnf. These commands are for inspection; avoid deleting individual files manually unless you know exactly what they are and why they need to be removed.

Clean Repository Metadata

Repository metadata describes the packages available from software repositories, including package names, versions, dependencies, and download locations. To remove cached metadata, run:

sudo dnf clean metadata

This is useful if repository information appears outdated or a repository has changed. DNF will retrieve fresh metadata when it next needs it. The cleanup can make the next package operation slower, especially on a slow connection or when several repositories are enabled.

To refresh metadata deliberately after cleaning it, run:

sudo dnf makecache

The makecache command downloads and prepares metadata for enabled repositories. On systems where you want DNF to refresh metadata as part of the command, you can also use:

sudo dnf makecache --refresh

Use the refresh option when you specifically want to check repositories for current data rather than rely on metadata that may still be considered valid.

Expire the DNF Cache

If you want DNF to treat cached repository metadata as out of date without immediately deleting every cached file, run:

sudo dnf clean expire-cache

This tells DNF that repository metadata should be refreshed the next time it performs an operation that needs it. It can be a convenient first step when you suspect that package lists are stale. It is different from cleaning all cache data: the command focuses on expiration, and DNF determines when to fetch updated information.

You can also request fresh repository data for a particular operation with --refresh. For example:

sudo dnf --refresh upgrade

This asks DNF to refresh metadata before attempting the upgrade. It does not replace every use of dnf clean all, but it may be enough when the issue is simply outdated repository information.

Clean DNF Database Cache

DNF maintains database cache files based on repository metadata. If those files are damaged or out of sync, cleaning the database cache may help. Run:

sudo dnf clean dbcache

DNF can regenerate the database cache from repository metadata when needed. If you are troubleshooting a repository issue, you may combine this with a metadata cleanup:

sudo dnf clean dbcache metadata

For a broad reset, use sudo dnf clean all instead. A full cleanup is simpler, but it removes more cached data and may require larger downloads afterward.

How to Clean DNF Cache on DNF5 Systems

Some newer Fedora releases use DNF5, while other distributions and releases continue to use the traditional DNF command. In many environments, the familiar cleanup commands work with the dnf command:

sudo dnf clean all

If your system provides a separate dnf5 executable, check its built-in help before running a command. For example:

dnf5 clean --help

Help output is the best way to confirm which cleanup options your installed version supports, since commands can differ across package-manager versions and distributions. Avoid assuming that every DNF-related command or option is identical on every Linux system.

How to Clear DNF Cache Automatically

DNF generally manages repository metadata according to its configuration, but downloaded package files can remain available until they are cleaned. If you regularly run low on space, you can make cache cleanup part of your maintenance routine. For example, run sudo dnf clean packages after a large installation or upgrade when you no longer need the downloaded RPMs.

Some distributions also provide configuration options or tools for removing cached files automatically. Configuration varies, so review your distribution’s documentation before changing DNF settings. Avoid adding an automatic cleanup task without considering whether you need cached packages for offline use, recovery, or repeated installations.

For occasional housekeeping, a manual cleanup is usually straightforward. Check cache size first, decide whether you need to preserve downloaded package files, and choose the narrowest cleanup command that meets your needs.

When Should You Clean the DNF Cache?

Cleaning the cache is not normally required after every package installation or update. It is most useful in a few specific situations:

  • You need to free disk space. Start by checking the size of /var/cache/dnf, then remove downloaded packages or all cache data as appropriate.
  • Repository information appears outdated. Expire or clean metadata, then refresh it with sudo dnf makecache.
  • A download or package operation was interrupted. Removing cached data and retrying can help if incomplete or inconsistent cache files are involved.
  • You changed repository configuration. Refreshing metadata helps DNF read the current repository information.
  • You are troubleshooting a DNF error. A targeted cleanup may help rule out stale metadata or database cache as the cause.

Cache cleanup is not a fix for every package-management problem. If DNF reports dependency conflicts, unavailable packages, repository authentication errors, or network failures, investigate those issues separately. Save the full error message before trying fixes so you can identify what actually changed.

Is It Safe to Delete the DNF Cache?

Using DNF’s own cleanup commands is generally safe. The commands remove cached information and downloads, not installed software. Your next package operation can rebuild the cache by downloading data from enabled repositories.

The main trade-off is convenience. After cleanup, DNF may need internet access to retrieve metadata and package files again. If you are preparing for offline maintenance, do not clear cached packages that you may need. Also, use dnf clean rather than manually removing the contents of /var/cache/dnf; the package manager can handle its own cache more reliably.

Common DNF Cache Cleanup Commands

  • sudo dnf clean all — removes all DNF cache data.
  • sudo dnf clean packages — removes downloaded package files.
  • sudo dnf clean metadata — removes cached repository metadata.
  • sudo dnf clean dbcache — removes generated database cache files.
  • sudo dnf clean expire-cache — marks repository metadata as expired.
  • sudo dnf makecache — downloads and prepares repository metadata.
  • sudo dnf --refresh upgrade — refreshes metadata before an upgrade.

For command-specific details on your system, run dnf clean --help or check the documentation supplied by your Linux distribution. The available options may depend on the installed DNF version.

Conclusion

To clean the DNF cache completely, run sudo dnf clean all. For a more targeted cleanup, choose packages, metadata, dbcache, or expire-cache depending on what you want to remove or refresh. Cleaning DNF cache files can reclaim disk space and help with stale-cache issues, but it does not uninstall software. Because DNF may need to download data again, keep cached packages if you rely on offline installs. Check your cache size, use DNF’s built-in cleanup commands, and refresh repository metadata when needed.


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 →