How to clean yum cache for untracked repositories?

There have been a few enhancements to the yum clean all workings in the past (most notably https://bugzilla.redhat.com/show_bug.cgi?id=1357083) but you are absolutely right that there are edge cases where yum clean all just doesn't do its job properly.

rm -rf /var/cache/yum, albeit nasty, does the trick every time.

The man page has a short message on cleaning untracked repos:

<...> Also note that untracked (no longer configured) repositories will not be automatically cleaned.

and

To purge the entire cache in one go, the easiest way is to delete the files manually. Depending on your cachedir configuration, this usually means treating any variables as shell wildcards and recursively removing matching directories. For example, if your cachedir is /var/cache/yum/$basearch/$releasever, then the whole /var/cache/yum directory has to be removed. If you do this, yum will rebuild the cache as required the next time it is run (this may take a while).

Regarding the last point about rebuilding taking a long time, you might want to follow rm -rf /var/cache/yum with && yum makecache to properly recreate the directories and avoid long waits on the next yum invocation. Note the difference between makecache and makecache fast though, most don't really know the difference. yum makecache fast only makes sure the repos are current. yum makecache actually downloads the metadata.


There is another option available:

# yum --enablerepo='*' --verbose clean all

Here it's explained:

# man yum
...
CLEAN OPTIONS
   The following are the ways which you can invoke yum in clean mode.
   Note that "all files" in the commands below means "all  files  in currently
   enabled repositories".  If you want to also clean any (temporarily)
   disabled repositories you need to use --enablerepo='*' option.

But the caches of removed repositories (which means "untracked") will be kept:

Disk usage under /var/cache/yum/*/* after cleanup:
4.0 k  enabled repos:
0      disabled repos
145 M  untracked repos