Skip to content
Latchkey

dnf clean all: Usage, Options & Common CI Errors

dnf clean all empties the dnf metadata and package cache to reduce image size.

dnf clean all is the Red Hat-family equivalent of clearing apt lists: it removes cached repo metadata and downloaded rpms so they do not bloat a Docker layer.

What it does

dnf clean all deletes cached repository metadata, downloaded packages, and dbcache from /var/cache/dnf (or /var/cache/yum). Running it in the same RUN layer as the install keeps the cached data out of the committed image.

Common usage

Terminal
dnf install -y git && dnf clean all && rm -rf /var/cache/dnf
yum install -y curl && yum clean all && rm -rf /var/cache/yum
dnf clean metadata        # clear only the repo metadata
dnf clean packages        # clear only downloaded rpms

Common errors in CI

A subtle one: if dnf clean all runs in an earlier Docker layer than the install, the cache is rebuilt in the install layer and the cleanup saved nothing - keep install and clean in the same RUN. After cleaning, a later dnf command re-downloads metadata, which can fail on a flaky mirror ("Failed to download metadata for repo"). For reproducibility some images also delete /var/cache/dnf explicitly since clean all leaves empty directories.

Options

SubcommandWhat it does
clean allRemove metadata, packages, and dbcache
clean metadataRemove only repository metadata
clean packagesRemove only cached rpm files
clean expire-cacheMark metadata expired (force refresh)

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →