Sub topic 13
Managing storage with dvc gc
Every dvc add, dvc repro and dvc exp run writes files into the DVC cache, and every dvc push copies them to the remote. Nothing is deleted when a pointer file changes. Old dataset versions, intermediate pipeline outputs and abandoned experiments pile up. This chapter covers how to find out what is safe to delete and how to delete it with dvc gc.
This chapter assumes you have read the chapters on the cache, remotes and switching versions. It also assumes you know Git branches and tags, and that you can read .dvc files and dvc.lock.
Why storage grows
DVC is append-only by design. When you change data/raw.csv and run dvc add again, the new content goes into the cache under its own hash. The previous version stays there, because a Git commit may still point to it. DVC cannot tell whether you still need an old version, because that depends on which Git commits you care about.
The same happens in pipelines. Each dvc repro that changes data/prepared or models/model.pkl stores the new output and keeps the old one. Experiments add more objects, since each dvc exp run caches its outputs.
You can see the growth directly:
du -sh .dvc/cache
dvc status -cdu shows how big the local cache is. dvc status -c compares the cache with the default remote and lists files that exist on one side only.
What "unused" means
dvc gc (garbage collection) deletes cached objects that nothing you name still references. DVC decides this by reading the .dvc files, dvc.yaml and dvc.lock in the Git revisions you select. It collects every hash those files mention. This is the set to keep. Everything else in the cache is unused.
So dvc gc never asks "is this file old?". It asks "does any selected commit point to this hash?". You pick the commits with scope flags.
| Flag | Keeps data referenced by |
|---|---|
| `-w`, `--workspace` | The files currently in your working directory, including uncommitted edits to `.dvc` files and `dvc.lock` |
| `-a`, `--all-branches` | The latest commit of every Git branch |
| `-T`, `--all-tags` | Every Git tag |
| `-A`, `--all-commits` | Every commit in the repository history |
| `--rev <commit>` with `-n <num>` | The given revision, plus the previous commits up to the number you set |
| `--all-experiments` | Every saved experiment |
You can combine flags. DVC keeps the union of everything selected.
Always preview first
--dry lists what would be removed and changes nothing. Run it before every real collection.
dvc gc --workspace --all-tags --dryIf the output includes files you expect to need later, widen the scope and dry-run again. Then run the real command:
dvc gc --workspace --all-tagsDVC asks for confirmation and states which scopes you chose. Pass -f (--force) to skip the prompt, which is useful in scripts but not in a first run.
Choosing a scope
The right scope depends on how your team uses Git.
- Cleaning your own laptop. Use
-w -a -T. You keep the data for your current files, every branch tip and every released tag. Old intermediate versions disappear. If you need one of them later,dvc pullcan fetch it again, provided the remote still has it. - Keeping recent history. Use
--rev main -n 10to retain roughly the last ten commits ofmain, and add-Tto keep releases. This suits a project that moves fast and rarely goes back past a few weeks. - Keeping everything reachable. Use
-A. This removes only objects that no commit ever referenced, such as outputs fromdvc addruns you never committed. It is the safest scope and frees the least space. - Minimal footprint. Use
-walone. This is the most aggressive scope. Every other branch and tag will need advc pullbefore you can check it out.
Because -w reads uncommitted pointer files, a half-finished edit to a .dvc file changes what gets kept. Commit or stash your work first, so the keep set is what you intend.
Cleaning the remote
By default dvc gc touches only the local cache. Add -c (--cloud) to delete unused objects from the default remote as well. Use -r <name> to target a different remote.
dvc gc --all-branches --all-tags --cloud --dry
dvc gc --all-branches --all-tags --cloudDeleting from the remote is the dangerous case. The remote is usually shared. Anything that nobody selected, and that exists only on the remote, is gone for good. Data that sits on a teammate's unpushed branch is not visible to you, so you cannot include it in the scope. If you run dvc gc -w -c and a teammate then checks out their branch, their dvc pull fails with missing files.
A safer routine for remote cleanup is:
- Ask the team to push all Git branches and run
dvc pushfor them. - Run
git fetch --alland check out nothing. Branch tips are read from refs, so a fetch is enough for-aand-T. - Dry-run with
-c, and read the output. - Run the real command from a clean working tree.
Local cache and remote are cleaned independently. Cleaning the local cache with dvc gc -w leaves the remote full. This is the normal setup for developers: small local caches, one big remote that a scheduled job or a project owner prunes.
Pipelines, experiments and the run cache
Pipeline outputs listed in dvc.lock count as references, like .dvc files. A cached models/model.pkl from last month survives if a selected commit's dvc.lock contains its hash. Outputs that were never committed, and experiments, are removed unless you select them. Experiments live under special Git references, not on branches. Add --all-experiments if you want their data kept. Without it, a plain -a -T run can delete the outputs of experiments you wanted to compare later with dvc exp show.
Metrics files declared with cache: false, like metrics.json in the earlier pipeline, are stored in Git and not in the cache. dvc gc has nothing to remove for them.
Multiple projects sharing one cache
Teams often point several repositories at one cache directory, for example with dvc cache dir /shared/dvc-cache. In that setup, a dvc gc run in one repository sees only that repository's references. It would delete objects that the other repositories still use. To protect them, list the others with -p (--projects):
dvc gc --workspace --all-tags --projects ../project-a ../project-b --dryDVC then reads the revisions of those repositories as well, and keeps the union. Without -p, do not share a cache directory between repositories you intend to garbage-collect independently.
Recovering from a mistake
What you can do depends on what was deleted.
- Local cache only. The data is probably on the remote. Run
dvc pull, ordvc fetchfollowed bydvc checkout, to restore it. - Remote and local cache. If another machine still has a populated cache, run
dvc pushfrom there. Otherwise you must regenerate the data. For pipeline outputs,dvc reprorebuilds them if the inputs exist. Raw data that came from outside the repository must be re-downloaded from its original source.
This is why many teams keep remote cleanup rare and manual, and keep raw datasets under a retention rule: never collected unless reachable-history scopes (-A) say so.
A worked routine
This sequence cleans a developer machine without losing anything that is recoverable:
git status # confirm a clean tree
dvc status -c # see what exists only locally
dvc push --all-branches --all-tags # make sure the remote has everything first
dvc gc --workspace --all-branches --all-tags --dry
dvc gc --workspace --all-branches --all-tags
du -sh .dvc/cache # confirm the space was freedPushing first is the safety step. After it, every object the selected commits reference exists on the remote, so the local deletion is reversible with dvc pull.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `dvc gc` errors and asks for a scope flag | No `-w`, `-a`, `-T`, `-A` or similar was given | Add the scope you want |
| `dvc checkout` or `dvc pull` reports missing cache files after cleanup | The commit you checked out was outside the gc scope, and the remote lacks the data | Run `dvc pull` if the remote has it, otherwise rebuild |
| Almost nothing is freed | The scope was too wide (`-A`) | Narrow the scope, for example `-w -T` |
| Teammate's branch loses data after `-c` | Their branch was not pushed or fetched when you ran gc | Restore from their cache with `dvc push`, and coordinate before the next cloud run |
| Experiments vanish from `dvc exp show` outputs | `--all-experiments` was not set | Add the flag next time |
Exercise
Your project has a main branch, a branch feature/new-labels that you have not merged, and tags v1.0 and v2.0. You are on main. You run dvc gc -w -c and then switch to feature/new-labels and run dvc pull. What happens, and what command should you have run?
Show answer
The pull fails with missing files. The scope -w kept only the data that the current workspace on main references, and -c deleted everything else from the remote, including the objects used only by feature/new-labels and the old tags. A safer command is dvc gc -a -T -c --dry, followed by the real run once the preview looks right. It keeps the tip of every branch and every tag. Pushing all branches and running dvc push -a -T first protects against data that exists only locally.
Exercise
Why does dvc gc -A free the least space of all the scope options, and when is it the right choice?
Show answer
-A keeps the data referenced by every commit in the repository history, so it removes only objects that no commit ever pointed to, for example outputs from a dvc add that was never committed. It is the right choice when you must be able to check out any past commit and restore its data, or when you want a conservative cleanup for a shared remote.