RackNerd Billboard Banner

How to Clean the DNF and APT Caches in Linux

Over time, your Linux system can accumulate a lot of cached package data. These files are stored by your package manager—DNF for Fedora-based distributions and APT for Debian-based systems. If left unchecked, they can eat up disk space. Cleaning the cache is an easy way to free up space without affecting system performance.

Here’s how to do it, step by step.


Why Clear the Cache?

When you install or update packages, your package manager downloads the necessary files and stores them in a local cache. This can be useful for reinstalling packages without re-downloading, but over time it adds up. Cleaning it out can:

  • Reclaim disk space
  • Remove outdated or unnecessary files
  • Keep your system tidy

Let’s go over how to clear the cache for each package manager.


Cleaning the DNF Cache (Fedora, RHEL, CentOS)

DNF stores its cache in /var/cache/dnf/. Here’s how to manage it:

Show Cache Usage

sudo du -sh /var/cache/dnf

Clean All Cached Data

sudo dnf clean all

This removes everything: metadata, package files, and any temporary data.

Clean Only Package Files

sudo dnf clean packages

This removes only the downloaded .rpm files but leaves metadata intact.

Clean Only Metadata

sudo dnf clean metadata

This keeps package files and deletes repository metadata. It’s useful when repos change and you need a fresh sync.


Cleaning the APT Cache (Ubuntu, Debian, etc.)

APT stores its cache in /var/cache/apt/archives/.

Show Cache Usage

sudo du -sh /var/cache/apt/archives

Clean All Cached Packages

sudo apt clean

This removes everything from the cache directory.

Remove Only Obsolete Packages

sudo apt autoclean

This deletes package files that can no longer be downloaded (old versions).

Remove Unused Dependencies

sudo apt autoremove

This removes packages that were installed as dependencies but are no longer needed.


Automate It (Optional)

To make cache cleanup part of your regular system maintenance, consider adding it to a cron job or systemd timer if you’re comfortable automating tasks.


Final Thoughts

Clearing the DNF or APT cache is safe and simple. It’s not something you need to do every day, but running a cleanup every few weeks or months can keep your system lean and clean—especially on systems with limited storage.

Got questions or want to automate cleanup? Drop a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

RackNerd Billboard Banner
Copy link