Sub topic 13
Managing Storage with `dvc gc`
This chapter assumes that you know basic Git commands and have used DVC to track data, models, and pipeline outputs. You should also understand that Git stores small DVC pointer files, while DVC stores the real file contents in a local cache and possibly a remote.
Why DVC storage needs cleanup
DVC keeps cached files by their content hash. If you change a dataset, DVC usually keeps both the old and new versions in the cache. This makes it possible to switch versions without downloading the same content again.
Over time, the cache can become much larger than the files currently used by your project. Old experiment outputs, deleted branches, and abandoned model versions may still occupy storage.
dvc gc means DVC garbage collection. It removes cached data that is not protected by the Git references you select. A Git reference is a name that points to a commit, such as a branch or tag.
The command can clean the local cache, a DVC remote, or both.
Choosing what to protect
The scope of garbage collection matters. A narrow scope protects less history and frees more space. A broad scope protects more versions but frees less space.
| Scope | What it protects | Typical use |
|---|---|---|
| Workspace | Data used by the current checkout | Clean a disposable local cache |
| All branches | Data referenced by every Git branch | Keep versions used by active development |
| All tags | Data referenced by Git tags | Keep releases and milestones |
| All commits | Data referenced by the full Git history | Preserve reproducibility for historical commits |
Before running cleanup, inspect your branches and tags:
git branch --all
git tag
dvc statusA version that is not visible in the selected Git references can become eligible for removal. If you later need that version, you may need to restore it from another cache or remote.
Cleaning the local cache
To remove local cache files that are not needed by the current workspace, run:
dvc gc -wThe -w option means workspace. This command is useful on a temporary checkout or a machine with limited disk space. It can remove data needed by another branch that is not currently checked out.
To protect data referenced by all branches and tags, use:
dvc gc -a -THere, -a includes all commits, and -T includes tags. This is safer for a shared development machine because historical versions associated with those references remain available.
Run garbage collection only after checking that important work has been committed. An uncommitted DVC pointer or an unpushed experiment may not be protected by the Git references you selected.
Cleaning a DVC remote
Remote storage can also accumulate data from deleted branches and experiments. To clean the configured remote, add -c:
dvc gc -a -T -cThis command uses the same reference scope for the remote. Be careful when several repositories share one remote. A file that appears unused in your repository might still be needed by another repository or by a release process.
A safer policy is to keep protected release tags and clean only after confirming that every required version has a tag or another documented recovery path.
A practical cleanup routine
Use this sequence for a project that has a shared remote:
- Commit all required DVC pointer files and pipeline metadata.
- Push important data with
dvc push. - Create or update tags for releases that must remain reproducible.
- Review branches, tags, and storage ownership.
- Clean a local cache with the smallest suitable scope.
- Clean the remote only after confirming that other users do not depend on the unprotected data.
git status
git branch --all
git tag
dvc push
dvc gc -a -TThe final command cleans the local cache while protecting data referenced by all commits and tags. Add -c only when you have approved remote cleanup.
Exercise
You have a release tag called v2.0, but your current workspace uses an experimental dataset. Which command protects all commits and tags while cleaning only the local cache?
Show answer
Run dvc gc -a -T. Do not add -c, because that would also clean the configured remote.