Aller au contenu

Leçon 23 · Troubleshooting and Practices

Clean DVC Storage Safely and Best Practices

Reclaim space with dvc gc without losing history, share a cache safely, structure DVC projects well and follow practical rules for security and reproducibility.

Versioning keeps old data, so storage grows. At some point you need to clean up. Cleanup is also the easiest place to destroy something important, so this lesson goes slowly. It ends with practices that keep projects healthy.

How dvc gc decides what to keep

dvc gc (garbage collection) removes cache objects that are not referenced by the versions you choose to keep. It does nothing unless you choose a scope.

Scope flag Keeps data referenced by
-w The current workspace
-a All Git branches
-T All Git tags
-A All commits
--rev <commit> A specific commit
-n <num> The last N commits, counted from --rev or branch tips (use with --rev or -a)
--all-experiments All DVC experiments
--date <YYYY-MM-DD> Commits made on or after that date (check dvc gc --help before relying on it)

Anything not covered by your scopes may be removed. For example, -w alone drops data that only older commits need. -A does not include experiments, so add --all-experiments if you need them.

Terminal
# Read-only: preview before you delete
dvc gc -w -a -T --dry
dvc gc -w -a -T

Without -f, DVC asks you to confirm.

Local versus remote cleanup

By default, gc cleans the local cache. You can restore removed local objects with dvc fetch or dvc pull only if they were pushed. With -c (--cloud), DVC also deletes from remote storage. The documentation calls this dangerous: deletion is irreversible unless you have another remote or a backup with the same data. -r chooses which remote to clean.

Before any cloud cleanup:

  1. Fetch all branches and tags from Git, so DVC can see them.
  2. Check whether other projects share the same remote or cache. The -p option lets you name other projects whose data must be kept.
  3. Run with --dry and read the list.
  4. Confirm that backups or storage versioning exist.
  5. Get explicit approval from the data owner.

Also consider retention duties: some data must be kept, and some (for example personal data) must be deleted on request. DVC does not manage either rule for you.

A shared cache

On a shared server, several users or projects can reuse one cache to avoid duplicate copies.

Terminal
# Read-only: shared cache setup
dvc cache dir /srv/shared/dvc-cache
dvc config cache.shared group
dvc config cache.type symlink
dvc checkout --relink

Put users in one group with proper permissions on the folder. Linked files are protected, so use dvc unprotect before editing. Running gc from one project on a shared cache can delete data another project needs, so coordinate.

Try the idea

This stand-in shows why scopes matter: it lists what would be removed under different choices.

Python Stand-in: what would gc remove?

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that even keeping all commits would remove the object that only an experiment uses.

Project structure

A readable layout helps everyone:

TEXT
project/
  data/            raw and processed data (DVC)
  src/             code (Git)
  models/          trained models (DVC outputs)
  reports/         metrics and plot data
  params.yaml
  dvc.yaml
  dvc.lock
  requirements.txt
  README.md

Best practices

  • Keep raw data immutable. Create new files from raw data; do not edit it in place.
  • Make each stage do one job with clear inputs and outputs.
  • Pin versions of DVC and libraries, and record the Python version.
  • Set seeds and note sources of randomness.
  • Push data with code, and tag releases.
  • Keep secrets in local config or a secret manager.
  • Document datasets: origin, license, owner, columns, personal data.
  • Back up your storage and limit who can delete.
  • Test pipelines on a small sample before long runs.
  • Review frozen, persist and always_changed regularly.

Stop using DVC for one file

To stop tracking a dataset, run dvc remove <file>.dvc. The data stays in the workspace, and the ignore entry is removed. To reclaim cache space, use dvc gc with the right scopes, remembering that older commits may still reference the data.

Common mistake: dvc gc -w -c to "tidy up". It can delete from remote storage versions that are not in your current workspace, including data that older commits and teammates need.

A cleanup plan

Write a short plan before you delete anything, even locally:

  1. Which space do you need to free: laptop, shared cache or remote storage?
  2. Which versions must survive? Think of releases, audit requirements and open experiments.
  3. Which scope flags express that? Write the exact command.
  4. What does --dry list, and does anything look surprising?
  5. How would you recover if you were wrong?

If you cannot answer the last question, keep the data. Disk space is usually cheaper than a lost model that cannot be rebuilt. For routine local cleanup, make sure that dvc push has completed first, so removed cache objects can be downloaded again.

Make growth visible

Cleanup is less stressful when it is not an emergency. Check disk and storage usage from time to time, and ask which outputs cause the growth. Often one large intermediate file dominates. You might split it, stop keeping it in the cache, or recreate it from earlier stages when needed. Weigh the storage you save against the time and certainty you lose, because a rebuilt file is only identical if the pipeline is deterministic. Agree on a retention rule with the data owner, write it in the README, and apply it consistently rather than deciding under pressure.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does dvc gc do if you give no scope flag?

    At least one scope is required.
  2. 2. Why is dvc gc with -c especially risky?

    Without a backup or other copy, deleted remote objects are gone.
  3. 3. You keep all commits with -A. Are experiment-only objects protected?

    The all-commits scope excludes experiments.
  4. 4. Which is a safe first step before cleanup?

    A dry run shows what would be removed without deleting anything.

Summary

  • dvc gc removes objects not covered by your chosen scopes.
  • Preview with --dry, and treat cloud cleanup as irreversible.
  • Coordinate gc on shared caches and remotes.
  • Keep raw data immutable, document datasets and protect secrets.
  • Retention and deletion rules are your responsibility.

Next: Project: A Versioned Dataset

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.