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
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 rpmsCommon 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
| Subcommand | What it does |
|---|---|
| clean all | Remove metadata, packages, and dbcache |
| clean metadata | Remove only repository metadata |
| clean packages | Remove only cached rpm files |
| clean expire-cache | Mark metadata expired (force refresh) |