~/ ~/documents ~/software ~/pictures github (opens in new tab)

Take Out the Rubbish

New Nix users are often surprised when deleting a file, uninstalling a package, or switching generations does not free disk space. The Nix store is not a temp folder that forgets what you touched. It is a graph of build artifacts, and cleanup is driven by graph reachability, not intuition.

Nix keeps a store path alive as long as it is reachable from a garbage collection root. If something still points at a path, that path is live and cannot be deleted. Common roots include:

This is why Nix can offer safe rollbacks and reuse, and also why disk usage grows when roots accumulate.

If Nix keeps something you expected to be gone, the answer is usually simple: somewhere, something still points at it.

One frequent culprit is the result symlink created by nix build:

nix build .#package
ls -l result

That symlink points into /nix/store. As long as it exists, the target is considered live. Remove it when you no longer need it:

rm result

If nothing else references that path, it becomes eligible for garbage collection.

After checking roots, run garbage collection:

nix-collect-garbage

Useful options:

You can also automate cleanup so maintenance does not depend on memory:

# In your nix.conf or configuration.nix
gc = {
    automatic = lib.mkForce true;
    dates = lib.mkForce "weekly";
    options = lib.mkForce "--delete-older-than 7d";
};

# Minimum free space before triggering GC
min-free = builtins.toString (30 * 1024 * 1024 * 1024); # 30 GB
min-free-check-interval = lib.mkForce 1;

Sometimes you also inherit dangling symlinks that point into /nix/store from old builds or manual operations. These do not always point to valid targets, but they are worth finding and cleaning up to keep your workspace tidy:

find ~ -type l -lname '/nix/store/*' \
    ! -exec test -e {} \; -print

If disk usage is not dropping, run this quick checklist before assuming GC is broken:

Nix is conservative on purpose: it deletes only unreachable paths. That safety-first behavior is what keeps rollbacks reliable, even when it occasionally feels stubborn.