Sub topic 4
How the DVC Cache Stores Your Files
Assume you know basic Git commands, can use a terminal, and understand YAML and simple Python projects. This chapter focuses on how DVC stores tracked files locally and connects them to pointer files and remote storage.
Why DVC uses a cache
Git stores the complete contents of every committed file. That works well for source code, but large datasets, images, and trained models can make a repository slow and expensive. DVC keeps large file contents outside normal Git history.
When you run dvc add, DVC does two related things:
- It calculates a hash for the file contents.
- It stores the contents in the local DVC cache.
- It creates a small
.dvcfile containing the file's hash and metadata. - It adds an ignore rule so Git does not track the original large file directly.
The hash identifies the contents, not the file name. If two files have identical contents, DVC can store one cache object and refer to it twice. This is called content-addressed storage.
The workspace, pointer, cache, and remote
A DVC project has several representations of the same data:
| Representation | Purpose | Usually tracked by |
|---|---|---|
| Workspace file | The usable dataset or model on disk | DVC |
| `.dvc` pointer file | Records the content hash and metadata | Git |
| Local cache object | Reusable local copy of the contents | DVC |
| Remote object | Shared copy for other machines | DVC remote |
For example, after dvc add data/raw.csv, the repository may contain data/raw.csv.dvc and .gitignore, while the actual CSV contents live in .dvc/cache.
A pointer file is small enough for Git to version. A simplified pointer might look like this:
outs:
- md5: 4f2c8d3a7a0b...
size: 18432
path: raw.csvThe exact fields can vary by DVC version and configuration. The important field is the content hash. It tells DVC which cache object belongs in the workspace.
How hashes prevent duplicate storage
DVC computes a checksum from the file contents. A checksum is a short value produced by a hashing algorithm. Changing even one byte normally produces a different checksum.
A cache path is derived from that checksum. In many installations, file objects appear below a path such as .dvc/cache/files/md5. The path layout is an implementation detail, so scripts should use DVC commands rather than manually reading cache paths.
dvc add data/raw.csv
dvc status
dvc cache dir
dvc list . --dvc-onlydvc cache dir shows the configured cache directory. dvc status compares the workspace with the pointer file and reports whether the file changed. The cache remains useful even when the workspace file is removed, because DVC can restore it from the cached object.
Links between the workspace and cache
When DVC materializes a cached file in the workspace, it may use a copy, hard link, symbolic link, or filesystem reflink. The selected method depends on the operating system, filesystem, and DVC configuration.
This optimization can save disk space and make checkout faster. Do not edit files inside .dvc/cache manually. Treat the cache as DVC-managed storage. Edit the workspace file, then run dvc add again to record its new contents.
If the workspace changes, the old cache object can remain available because another pointer, experiment, or commit may still refer to it. This allows DVC to switch between versions without downloading data that is already cached.
Cache cleanup
Over time, the cache can contain objects that are no longer referenced by the Git revisions, branches, or experiments you care about. DVC can identify and remove unused objects with dvc gc.
Review the command's scope before deleting anything. A cleanup limited to the current workspace can remove objects needed by another branch or teammate if those objects are not represented in the selected scope.
dvc gc --workspaceUse broader scopes only when you understand which Git revisions and DVC projects must remain restorable. Remote cleanup also requires care because deleting a remote object can prevent another machine from checking out an older version.
Exercise
A dataset is unchanged, but its filename changes from data/train.csv to data/archive/train.csv. Will DVC necessarily store a second copy of the contents?
Show answer
No. DVC addresses cache objects by content hash, not filename. If the contents are identical, DVC can reuse the existing cache object, although the pointer path changes.