How to clean Gentoo distfiles

Gentoo distfiles can grow quietly because Portage keeps source archives after packages finish building. Cleaning them frees disk space without unmerging installed packages, but an overly aggressive pass can force later rebuilds to download sources again or lose convenient downgrade archives.

Portage stores these archives in DISTDIR, which defaults to /var/cache/distfiles on current installations and can be changed in /etc/portage/make.conf. The eclean-dist command from app-portage/gentoolkit removes source archives that Portage no longer needs according to the selected protection mode.

The conservative run keeps sources for ebuilds still available in the repository. The –deep mode saves more space by protecting only distfiles tied to currently installed package versions, so use a pretend run first and avoid it when offline rebuilds, fetch-restricted source archives, or quick downgrade room matter.

Steps to clean Gentoo distfiles:

  1. Open a terminal on the Gentoo system with sudo privileges.
  2. Install gentoolkit if eclean-dist is not available.
    $ sudo emerge --ask app-portage/gentoolkit

    eclean-dist is installed by app-portage/gentoolkit. Skip this step when the command is already present.

  3. Print the active DISTDIR path.
    $ portageq envvar DISTDIR
    /var/cache/distfiles

    Use the printed path in the size checks below when the system uses a custom DISTDIR.

  4. Check how much space the distfiles directory uses before cleanup.
    $ du -sh /var/cache/distfiles
  5. Preview the conservative cleanup.
    $ sudo eclean-dist --pretend

    –pretend lists files that would be removed without deleting them. If it reports that the distfiles directory is already clean, no conservative cleanup is needed.

  6. Remove obsolete distfiles with the conservative mode.
    $ sudo eclean-dist

    This deletes cached source archives that eclean-dist considers obsolete. Installed packages remain installed, but future rebuilds may need to download removed source archives again.

  7. Preview a deep cleanup only when more space must be recovered.
    $ sudo eclean-dist --deep --pretend

    –deep protects only distfiles for currently installed package versions. It can remove source archives that would have helped with downgrades or reinstalling packages that were removed earlier.

  8. Run the deep cleanup if the pretend list is acceptable.
    $ sudo eclean-dist --deep

    Skip this step on systems that need a wider local source cache for offline rebuilds, fetch-restricted packages, or rollback testing.

  9. Verify that eclean-dist has no further distfiles to remove.
    $ sudo eclean-dist --pretend
     * Building file list for distfiles cleaning...
     * Your distfiles directory was already clean.

    If eclean-dist –deep reports unavailable installed packages, remove only packages that are truly no longer needed and then rerun the cleanup. Related: How to remove a Gentoo package

  10. Check the distfiles directory size after cleanup.
    $ du -sh /var/cache/distfiles