Both DNF and APT — the package managers for Red Hat and Ubuntu-based Linux distributions — store cached information to ensure the software installation process is much faster and more reliable. With these caches in place, neither package manager has to download the information whenever you attempt to update, upgrade, or install software.
But sometimes, that cache information can become outdated or corrupted. When that happens, you might find that the DNF of APT doesn’t function properly.
What do you do? You clean the cache, which will delete all of that information, so you have a clean slate.
How do you clean those caches? Let me show you.
SEE: Debian vs Ubuntu: Which Linux Distro Fits Your Needs Best? (TechRepublic)
How to clean the cache for the DNF package manager
The best way to clean the DNF cache is by running the command:
sudo dnf clean dbcache
This will remove all cache files generated from the repository metadata.
If that doesn’t solve your problems, you can run a complete clean with the command:
sudo dnf clean all
SEE: Top Commands Linux Admins Need to Know (TechRepublic Premium)
How to clean the cache for the APT package manager
With the APT package manager, you can issue the command:
sudo apt-get clean
This will remove the content from /var/cache/apt/archives (except for the lock file).
Another APT option is to run:
sudo apt-get autoclean
This only removes the packages that cannot be downloaded from repositories. In other words, if you downloaded a .deb package and installed it (as opposed to installing it from a remote repository), any file associated with that package will remain.
And that’s all there is to cleaning the package manager caches for both Red Hat and Ubuntu-based distributions. You probably won’t have to use these tools, but if you do, you know how.
This article was originally published in June 2021. It was updated by Antony Peyton in January 2025.