skip to content

How does garbage collection in the open-source Distribution registry decide which blobs to delete, and why should writes stop while it runs?

level: middleimportance: should knowfreq 50%

answer

  1. references, not age
  2. mark set of digests
  3. sweep everything unmarked
  4. blobs before the manifest

basics

~20 s

Distribution registry garbage collection marks every digest that any manifest references, then sweeps and deletes every blob not marked. Writes must stop because a push uploads blobs before its manifest, so in-flight layers look unreferenced and get swept.

solid answer

~40 s

In the open-source Distribution registry, garbage collection is a two-phase, reference-based pass. In the **mark** phase it scans every manifest and collects the digests they reference; in the **sweep** phase it deletes every blob whose digest is not in that set. It never judges age: content becomes eligible only after a manifest is deleted through the API, which needs `storage.delete.enabled: true` (off by default), or when `--delete-untagged` removes manifests no tag references. The registry should be in `readonly` maintenance mode or stopped, because a push uploads its layers before its manifest; those layers are unreferenced during the run and would be swept, leaving a corrupted image. Use `--dry-run` first. Harbor avoids the window by skipping files uploaded in the last two hours.

code

yaml · 8 lines
yaml
storage:
  filesystem:
    rootdirectory: /registry/data
  delete:
    enabled: true
  maintenance:
    readonly:
      enabled: true

go deeper

for a junior

Recall that collection removes blobs nothing references, and that something must first delete an image or leave it untagged before its layers can go.

for a middle

Walk through mark and sweep, the default-off delete setting, what --delete-untagged adds, and why an in-flight push loses its layers if writes are not stopped.

for a senior

Plan the window: read-only maintenance mode, a dry run reviewed beforehand, and how Harbor's two-hour reservation changes the trade-off between availability and promptly reclaimed space.

for a principal

Decide how often reclaiming space is worth a write outage, and whether that cost argues for a registry that collects online.

## What garbage collection is for An OCI registry stores content by digest. Layers, configuration blobs and the manifests themselves are all **blobs** addressed by the hash of their bytes, and a layer is stored once no matter how many manifests reference it. That sharing is why deleting an image cannot simply delete its files: a layer may still belong to another image. In the open-source Distribution registry, **garbage collection** is the process that removes blobs from storage once no manifest references them any more. It is a command built into the registry binary, not something the registry does on its own in the background. ## Step zero: making content eligible Garbage collection never decides on its own that an image is old. Something must first remove references: - A client deletes a manifest with `DELETE /v2/<name>/manifests/<digest>`. In this registry that requires `storage.delete.enabled: true`, which **defaults to false**; with deletion disabled, the distribution spec requires a `400` or `405` response. - A deleted manifest can no longer be read through the API, and its layers become eligible if no other manifest uses them. - Deleting only a tag (`DELETE /v2/<name>/manifests/<tag>`, where a registry supports it) removes a name. The manifest it pointed at still exists, untagged. ## The two phases: mark and sweep 1. **Mark.** The collector scans every manifest in the registry and builds a set of the digests they reference. This **mark set** is the list of blobs not to delete. 2. **Sweep.** It scans every blob in storage and deletes each one whose digest is not in the mark set. The source's own example: manifest A references layers `a` and `b`, manifest B references `a` and `c`. After B is deleted, a collection removes `c` and B's own manifest blob, while `a` survives because A still references it. Two flags change what the run does: - `--dry-run` prints the progress of both phases and the blobs eligible for deletion, without removing anything. - `--delete-untagged` also deletes manifests that no tag currently references, so their layers can be swept in the same run. ```bash bin/registry garbage-collect --dry-run --delete-untagged /path/to/config.yml ``` ## Why writes must stop The registry documentation is blunt: run collection with the registry in **read-only mode** or not running at all. The reason is the order of a push. A client uploads every blob first and the manifest last. While a push is in progress, its freshly uploaded layers are referenced by no manifest yet, so they are not in the mark set, and the sweep deletes them. When the manifest then arrives, it points at layers that are gone: a corrupted image. This is why the method is called **stop-the-world** collection. The registry provides the switch for it: ```yaml storage: delete: enabled: true maintenance: readonly: enabled: true ``` The documented sequence is to restart the registry with read-only enabled, run the collector, and restart again with it removed or set to false. Pulls keep working during the window; pushes fail. ## How Harbor differs Harbor runs garbage collection **without** a read-only window: pushes, pulls and deletes continue while it runs. It protects in-flight uploads differently, by not sweeping manifest and blob files whose timestamp falls within a **two-hour window**. Its other controls map onto the same ideas: - **DRY RUN** prints the blobs eligible for deletion and an estimate of the space it would free; - **Allow garbage collection on untagged artifacts** deletes untagged artifacts first and then collects their blobs, the equivalent of `--delete-untagged`; - runs can be scheduled hourly, daily, weekly or by cron, and stopping a run does not restore anything already collected. ## Not to be confused with upload purging The Distribution registry also has **upload purging** under `maintenance`, enabled by default, which removes orphaned files from abandoned uploads. Its defaults are an `age` of `168h` and an `interval` of `24h`. It cleans half-finished uploads; it never touches committed blobs, and it does not replace garbage collection. ## Summary | Question | Answer in the open-source Distribution registry | |---|---| | What gets deleted? | blobs whose digest no manifest references | | What decides "old"? | nothing; only references count | | Can it run during pushes? | no; read-only or stopped | | How do you preview? | `--dry-run` |

  • Why does deleting only a tag not let the collector free that image's layers?
    Removing a tag removes a name; the manifest it pointed at still exists, untagged. The mark phase scans every manifest, so it still marks that manifest's layers as referenced. The layers become eligible only when the manifest itself is deleted by digest, or when the collector runs with --delete-untagged.
  • How does Harbor run garbage collection without a read-only window?
    Harbor does not sweep manifest and blob files whose timestamp falls within a two-hour window, so the layers of a push still in progress are left alone. Pushes, pulls and deletes continue during the run. The cost is that very recent orphans wait for a later run.

  • Registry garbage collection deletes images older than a configured age.
  • Deleting a manifest through the API frees its layers' disk space at once.
  • A layer shared with another image is deleted when one image is removed.
  • It is safe to keep pushing while the registry's garbage-collect command runs.
  • Manifest deletion is enabled by default in the open-source Distribution registry.