# `gc`

`kix gc` deletes Kix-managed resources that earlier deploys and rollbacks left
behind on the cluster. It has three modes: the default mode deletes leftover
resources recorded by deploys and rollbacks that ran without `--prune`,
`--full-scan` scans all live Kix-managed resources for the named cluster, and
`--operator` deletes retired Activation records so the Kix operator cleans up
after them. Every mode also retires old `Superseded` Activation records.

`kix gc` keeps every resource in the cluster's active deployment (the build
named on its `Active` Activation record from the last deploy or rollback).
Unapplied edits in your local files do not affect `kix gc`. To delete resources
you removed from your cluster definition, run `kix deploy --prune`, or run
`kix deploy` and then `kix gc`. After `kix rollback`, `kix gc` keeps the
restored build and deletes resources left behind by the build you rolled back
from.

This page is hand-maintained. Check `cli/kix-cli/src/cli.rs` and
`cli/kix-cli/src/commands/gc/{mod,plan,render}.rs` when in doubt.

## Synopsis

```text
kix gc <CLUSTER> [-y] [--dry-run] [--full-scan [--include unstamped,unattributed,copies]] [--operator]
       [--timeout <DURATION>] [--keep <N>] [--keep-max <N>] [--keep-for <DURATION>]
       [--flake <FLAKE>] [--context <CONTEXT>]
```

| Argument or flag | Meaning |
| --- | --- |
| `<CLUSTER>` | Cluster name to build from the flake. |
| `-y`, `--yes` | Skip the confirmation prompt. Required when stdin is not a terminal. |
| `--dry-run` | Print what gc would delete, both resources and Activation records, and delete nothing. |
| `--full-scan` | Scan the named cluster's resources carrying the Kix managed-by label and delete those whose identity hash is not in the active deployment's build (or the build from your local files if the cluster has no `Active` Activation record). |
| `--include <KINDS>` | With `--full-scan` (required), also judge the kinds of resource a full scan only lists by default. A comma-separated list of the values below. |

| `--include` value | Resources it adds |
| --- | --- |
| `unstamped` | Resources written by Kix that carry the label without `kix.run/identity-hash`. Kix cannot match them to a build, so by default they are only listed. |
| `unattributed` | Resources with no matching Activation record on the cluster. Kix cannot tell which Kix cluster they belong to, so by default they are only listed. |
| `copies` | Resources with Kix's labels that Kix did not apply. Another controller or tool created or applied them, such as an operator copying a custom resource's labels onto a database volume claim or an Event. By default, copies are only listed. |
| `--operator` | Delete the retired Activation records only; the operator's finalizer removes their resources. |
| `--timeout <DURATION>` | How long to wait for a deleted resource while its finalizers do not change (`5m`, `300s`, or seconds). Default `3m`. Each removed finalizer resets the timer, up to 5 minutes in total per resource. Not with `--operator`. |
| `--keep <N>` | Always keep at least the `N` newest Superseded records, whatever their age. Default `10`. `0` keeps none. |
| `--keep-max <N>` | Never keep more than `N` Superseded records. Default `50`. A value below `--keep` is raised to it. |
| `--keep-for <DURATION>` | Beyond `--keep`, retire records older than this: `30d`, `12h`, `45m`, `30s`, or a bare number of days. `0` or `off` disables the age rule. Default `30d`. |
| `--flake`, `--context` | See [Global flags](/docs/reference/cli/global-flags/). `-o` has no effect. |

## Modes

| Mode | What `kix gc` checks | What is deleted |
| --- | --- | --- |
| Default | Kept orphans on the `Active` Activation record: resources listed in its kept-orphans [revision](/docs/reference/annotations-crds-and-receipts/#packageinstancerevision) (or builds in `status.orphansKeptFrom` on records written by earlier versions) | Those resources whose kind, namespace, and name are absent from the active deployment's build. If the cluster has no `Active` Activation record, `kix gc` prints a message and exits without deleting anything. |
| `--full-scan` | Every live Kix-managed resource for the named cluster | Resources whose identity hash is absent from the active deployment's build (or from the local files' build when the cluster has no `Active` Activation record). Any resource referenced by a revision of this cluster is deleted only if no revision other than the kept-orphans revision lists it. |
| `--operator` | Activation records | Retired records; the operator deletes their resources |

To load the active deployment's build, both the default mode and `--full-scan`
first check this machine's cache from the last `kix deploy` or `kix rollback`.
If the cached build does not match the `Active` record (for example, after
deploying a cluster of the same name in another Kubernetes context), Kix warns,
ignores the cache entry, and looks up the build in Kix's local store. If the
deployed build is not on this machine at all, `kix gc` builds your local files
and proceeds only when they produce that exact build; otherwise it stops,
deletes nothing, and prints both build hashes.

Both the default mode and `--full-scan` keep resources belonging to an
unfinished deploy of this cluster, including a failed deploy, a partial
[`kix deploy --only`](/docs/reference/cli/deploy/#deploy-one-instance-at-a-time)
run, or a deploy that is queued or running. `kix gc` identifies these
resources from the revisions those deployment records name (or, for a Kix
cluster an earlier version of Kix deployed last, from the records the
resource's `kix.run/activations` annotation names) and lists them under **Kept for an unfinished deploy**, grouped by
deployment record. Run `kix deploy <cluster>` to finish or replace that
deploy, then run `kix gc <cluster>` again. If the active deployment's build
is not on this machine and your local files match a `Partial` deployment
record from `kix deploy --only`, `kix gc` names that record in its error
message.

Default mode, `--full-scan`, and the Kix operator inspect only objects carrying the label `app.kubernetes.io/managed-by=kix`. `kix gc` never lists or deletes unlabelled objects, such as resources applied with `kubectl`. See [Objects Kix did not apply](/docs/reference/cli/diff/#objects-kix-did-not-apply).

`--full-scan` judges only the resources of the cluster you name. Each Activation
record carries a `kix.run/cluster` label and links to revisions listing that
deploy's resources, while the `Active` record's kept-orphans revision lists
leftover resources from deploys and rollbacks run without `--prune`. Keeping
those leftovers on the `Active` record ties them to this Kix cluster even after
`kix gc` retires the deployment record that created them, so `--full-scan`
deletes them without `--include unattributed`. On clusters not yet upgraded to
revisions, Kix reads the same ownership from `kix.run/activations` on live
objects and `status.orphansKeptFrom` on deployment records. A full scan
classifies each resource as follows:

- A resource that only other Kix clusters deploy is skipped, and
  `kix gc` prints how many it skipped and which Kix clusters they belong to.
- A resource that another Kix cluster also deploys is kept and listed as
  `kept ..., still deployed by Kix cluster <name>`.
- A resource that no deployment record on the cluster names is listed under
  "Objects with no matching deployment record" and kept by default. Pass
  `--include unattributed` to check these resources against the active
  deployment's build and delete any that are not in it. A resource may have no
  matching record because `kix gc` retired its records and no remaining
  record keeps it, because an earlier version of Kix applied it before it
  recorded deployment records on objects, or because it belongs to another Kix
  cluster whose records were deleted. This rule applies
  even when only one Kix cluster has records on the Kubernetes cluster, since
  missing records may have belonged to another Kix cluster.

The default mode, `kix deploy --prune`, and `kix rollback --prune` apply the same rule to
a resource both clusters deploy: when the named cluster's build drops it and
another cluster's current build still contains it, the plan lists it as kept.

Right before deleting each resource, `kix gc` reads it again and deletes it only
if it has not changed since that read and no other Kix cluster deploys it
(checked against other clusters' records, revisions, and `managedFields`). A resource left for
either reason is printed as `= prune: kept ...` and is not a failure.

Resources are deleted dependents first. Namespaces and
CustomResourceDefinitions are skipped. A resource that is already gone when
gc deletes it is reported, not counted as a failure.

Like `kix deploy --prune`, `kix gc` deletes most resources with `Background`
propagation. For custom resources whose CRD declaration sets `controller`,
`kix gc` uses `Foreground` propagation instead. That keeps the custom resource
on the cluster until Kubernetes deletes any owned child objects that block its
deletion. `kix gc` runs independent deletes in parallel and waits for each resource
to be removed from the cluster. If a resource is already being deleted (marked
in a `--full-scan` plan), `kix gc` waits for that deletion instead of sending a
second delete request. It stops waiting if no finalizer is removed within
`--timeout` (default `3m`), or after 5 minutes in total. If a resource is still on the cluster
when the wait ends, `kix gc` counts it as a failure, prints `[K2794]` (with a
`kubectl patch` command when finalizers remain), and keeps any resources
recorded as its dependencies in `kix.run/depends-on`. When a package sets
`controller` on a custom resource kind, that dependency list includes the
operator, and `[K2794]` reports whether the operator is ready, not ready, or
not on the cluster. Most packages do not set `controller` yet, so removing a
custom resource and its operator together can delete the operator before it
removes the custom resource's finalizer. A
slow cleanup may still finish on its own; run `kix gc` again later, or pass a
longer `--timeout` (up to `5m`).

A deploy or rollback without `--prune` keeps its orphans and records them in a
kept-orphans revision linked on the `Active` Activation record. Default mode
deletes those orphans (listed under "Objects that left the build in earlier
deploys") and updates the kept-orphans revision with any remaining orphans
(resources that failed to delete or whose kind Kix lacked permission to read)
so later runs still track them. On records written by earlier versions of Kix,
kept orphans are tracked as build paths in `status.orphansKeptFrom`, which
`kix gc` clears once deletion succeeds without unread builds or forbidden kinds.

If the kept-orphans revision linked on the `Active` record is missing or
unreadable, default `kix gc`, `kix deploy --prune`, and the Kix operator refuse
to delete resources. Run `kix gc --full-scan` to rebuild the kept-orphans
revision and complete the scan. Kix collects every live resource owned by this
Kix cluster (via `kix/<cluster>`, or via `kix` plus `kix.run/activations` on
objects not yet migrated) that is absent from both the active build and this
cluster's other revisions, links the rebuilt revision onto the `Active` record,
and runs the full scan. With `--dry-run`, it reports how
many resources the rebuilt list would contain without writing it. Deploying with
`--prune` every time keeps the list empty, so the default mode has no kept
orphans to find; see [`deploy`](/docs/reference/cli/deploy/).

Retiring a record removes it as a rollback target. A record is deleted only if
it has not changed since it was listed.

## Deploy lock and version checks

Before reading deployment records, `kix gc` checks the cluster's
[deploy lock](/docs/reference/cli/deploy/#the-deploy-lock) the same way
`kix deploy` does: if an active run holds the lock, `kix gc` stops; if the
lock's lease has expired, `kix gc` takes it over, cleans up, and releases it.
`kix gc` then records the lock generation alongside the deployment records
while building the deletion plan. After you confirm, `kix gc` takes the lock
and holds it until deletion finishes. If another run holds the lock or
modified the cluster after `kix gc` read the records (changing the lock
generation), `kix gc` stops without deleting anything. `kix gc --operator`
does not take the lock.

`kix gc` also stops before deleting anything if a newer minor version of Kix
last changed the cluster, or if running `kix gc` would skip a
[required stop](/docs/reference/cli/deploy/#required-stops). Unlike
`kix deploy` and `kix rollback`, `kix gc` has no downgrade override:

```text
Garbage collection blocked: cluster prod was last deployed with Kix 0.9.1, and this is Kix 0.8.4.
Nothing was deleted from cluster prod.
```

## Examples

<Command commands={[
  "kix gc demo",
  "kix gc demo --dry-run",
  "kix gc demo --full-scan --yes",
  "kix gc demo --full-scan --include unattributed,copies",
  "kix gc demo --keep 5 --keep-for 14d",
]} />

## Exit status

| Code | Meaning |
| --- | --- |
| `0` | Nothing to collect, everything planned was deleted, `--dry-run` printed the plan, or the prompt was declined |
| `1` | A deletion failed or timed out, or `kix gc` left a planned object in place because an undeleted object depends on it or its dependencies form a cycle; confirmation was needed on a non-interactive terminal; another run holds the deploy lock or modified the cluster after `kix gc` read its records; a newer minor version of Kix last changed the cluster, or `kix gc` would skip a required stop; or a build or cluster error |

:::tip[Use it in a task]
See [Garbage-collect stale resources](/docs/how-to/operate-a-cluster/garbage-collect-stale-resources/)
for choosing a mode, and
[Configure activation retention](/docs/how-to/operate-a-cluster/configure-activation-retention/)
for the retention flags.
:::