# Garbage-collect stale resources

Use `kix gc` to delete resources that an earlier deploy or rollback left behind
on the cluster. Every mode also retires old Activation records according to your
retention policy.

`kix gc` keeps every resource in the cluster's active deployment (the build
named by its `Active` Activation record from the last deploy or rollback).
Editing your local files has no effect on `kix gc` until you deploy. To remove a
resource from the cluster, remove it from your cluster definition and run
`kix deploy --prune`, or run `kix deploy` and then `kix gc`.

## Review the normal garbage-collection plan

Run `gc` with the cluster name:

<Command commands={["kix gc how-to-application"]} cwd="kix-examples/" />

To see the plan without being asked to confirm, and without deleting
anything, add `--dry-run`:

<Command commands={["kix gc how-to-application --dry-run"]} cwd="kix-examples/" />

By default, `kix gc` deletes leftover resources from earlier `kix deploy` and
`kix rollback` runs that ran without `--prune`. Those runs record the builds
whose old resources they kept on the `Active` Activation record. `kix gc` checks
those builds, lists any resources no longer in the active deployment under
**Objects that left the build in earlier deploys**, shows the Activation records
it plans to retire, and asks for confirmation.

Check the package, kind, namespace, and name of each resource before approving
the plan. Kix deletes dependants before their dependencies.

A Namespace or CustomResourceDefinition that has left the build is listed
under **Kept** and is not deleted, in either mode. Deleting a Namespace deletes
everything in it, and deleting a CustomResourceDefinition deletes every custom
resource of that kind on the cluster, including ones Kix does not manage.
`kix deploy --prune` keeps the same kinds. When you do want one removed, delete
it with `kubectl delete` after checking what it contains.

## Run without a prompt

After reviewing the plan, pass `--yes` in automation:

<Command {...gcPlan} />

This performs the same comparison and deletion without asking for
confirmation. The captured run follows an instance being renamed in the
cluster definition and deployed without `--prune`, so Kix removes the resources
deployed under the old name.

## Scan the live cluster for orphans

Use `--full-scan` to scan the live cluster for stale Kix-managed resources that
default `kix gc` does not track, such as resources from a build that is not on
this machine, or from a deployment whose record was retired (see below):

<Command commands={["kix gc how-to-application --full-scan"]} cwd="kix-examples/" />

A full scan reads live resources carrying Kix ownership metadata and compares
them with the resources in the active deployment's build (or the build from
your local files when the cluster has no active deployment).

When one Kubernetes cluster hosts more than one Kix cluster, a full scan
judges only the named cluster's resources. It identifies them from the
revisions linked to each cluster's Activation records, skips the other
clusters' resources, and keeps any shared resource that another Kix cluster
still deploys.

Leftover resources from a deploy or rollback without `--prune` are still
deleted by a full scan even after `kix gc` retires the Activation record that
deployed them. Because the `Active` record's kept-orphans revision tracks
those leftovers, Kix knows they belong to this cluster and deletes them without
`--include unattributed`.

If no Activation record on the cluster references a resource, Kix cannot tell which Kix cluster owns it. A full
scan lists it under "Objects with no matching deployment record" and keeps it,
even when only one Kix cluster has records there. The resource may come from an
earlier deploy whose records
`kix gc` has retired, or from another Kix cluster whose records were deleted.
Review the listed resources, then pass `--include unattributed` to check them
against the active deployment's build and delete any not in it.

Resources written by Kix that carry a Kix label without a `kix.run/identity-hash`
annotation are listed under "No Kix build annotation" and kept by default,
because Kix cannot match them to a build. Include them only after checking each reported resource. `--include`
takes a comma-separated list, such as `--include unstamped,unattributed`:

<Command
  commands={["kix gc how-to-application --full-scan --include unstamped"]}
  cwd="kix-examples/"
/>

## When a delete does not finish

Kubernetes keeps a deleted object on the cluster until every finalizer on it is
removed. Usually, the controller or operator that added a finalizer removes it
after cleaning up external state, such as a database or cloud load balancer.
`kix gc` waits for each deleted object to disappear, stopping if 3 minutes pass
without a finalizer being removed or after 5 minutes in total.

If an object is still on the cluster when the wait ends, `kix gc` fails with
`[K2794]`, lists any remaining finalizers, and exits with code `1`. Kix also
keeps any resources recorded as dependencies of that object, because the object
still on the cluster depends on them; the next `kix gc` run deletes them once
the object is gone. When a package declares its operator as the `controller` for
a custom resource kind, Kix deletes custom resources of that kind before the
operator, keeps the operator until they are gone, and reports in `[K2794]`
whether the operator is ready. Most packages do not declare a `controller` yet,
so removing a custom resource and its operator together can delete the operator
before it removes the custom resource's finalizer.

To resolve `[K2794]`:

1. **Wait if cleanup is still in progress.** Slow cleanups, such as pods with a
   long termination grace period or a cloud load balancer teardown, can finish
   on their own after Kix stops waiting. Run `kix gc` again once the object is
   gone.
2. **Check the controller that owns the finalizer.** Verify that the controller
   or operator responsible for the finalizer is running. If you removed it in
   the same change, restore and deploy it so it can clean up its custom
   resources, then remove it and run `kix gc` again.
3. **Remove the finalizer manually to skip external cleanup.** Kix never removes
   finalizers itself. To make Kubernetes delete the object immediately, run the
   `kubectl patch` command printed in the `[K2794]` error message. Anything the
   finalizer would have cleaned up outside Kubernetes, such as an external
   database or cloud load balancer, is left behind. Run `kix gc` again afterward
   to delete any dependencies Kix kept.

Kix omits the `kubectl patch` command when an object has a built-in Kubernetes
protection finalizer, even alongside a controller finalizer, and instead
explains how to clear the condition safely. For example,
`kubernetes.io/pvc-protection` holds a PersistentVolumeClaim while a pod still
mounts it; stop those pods instead, because removing that finalizer on a claim
with reclaim policy `Delete` lets Kubernetes delete the underlying volume and
its data while a pod still uses it.

## Let the operator perform cleanup

For an operator-managed cluster, use operator mode:

<Command commands={["kix gc how-to-application --operator"]} cwd="kix-examples/" />

Kix deletes the Activation records selected by the retention policy. These are
all Superseded records, so the operator only removes its finalizer from each
one. It deletes no resources: the Active record owns them. The operator deletes
resources only when an Active, Deploying or Degraded record is deleted, and even
then it keeps every Namespace and CustomResourceDefinition. If any resource
fails to delete or is still on the cluster when the wait ends, the operator
keeps its finalizer on the Activation record, posts a `DeletionUnfinished`
Warning Event on it, and retries 30 seconds after each attempt finishes. See
[When an Activation record is deleted](/docs/reference/cli/operator-mode/#when-an-activation-record-is-deleted).

Use the same `--keep`, `--keep-max`, and `--keep-for` flags in any mode. For
example:

<Command
  commands={[
    "kix gc how-to-application --full-scan --keep 20 --keep-max 100 --keep-for 90d",
  ]}
  cwd="kix-examples/"
/>

## Remove everything a Kix cluster deployed

Kix does not have a single uninstall command. To remove a Kix cluster from a
Kubernetes cluster, deploy an empty set of packages with `--prune`, then delete
the remaining cluster-level objects with `kubectl`.

First, list the resources in the current build and note its `Namespace` and
`CustomResourceDefinition` names, since the next step removes them from the
build output:

<Command commands={["kix list resources how-to-application"]} cwd="kix-examples/" />

Next, remove every package from the cluster definition (keeping any flavor or
platform import the definition still needs) and deploy with `--prune`:

<Command commands={["kix deploy how-to-application --prune"]} cwd="kix-examples/" />

Kix deletes every resource from the previous deployment except:

- **Namespaces and CustomResourceDefinitions.** Kix never deletes these
  automatically. Deleting a Namespace deletes everything inside it. Deleting a
  CustomResourceDefinition deletes every custom resource of that kind across all
  namespaces, so check first that no other Kix cluster or tool uses it.
- **Runtime objects created by operators or workloads**, such as Secrets and
  Leases. Because Kix did not deploy them, it does not track them. Deleting
  their Namespace removes them.
- **Shared resources still deployed by another Kix cluster.** Kix leaves these
  in place for the other cluster.
- **Activation records** (the cluster's deployment history). List them with
  `kubectl get activations.kix.run -l kix.run/cluster=how-to-application`. If
  the Kix operator manages the cluster, deleting the `Active` record makes the
  operator delete that deployment's resources, as described in
  [Let the operator perform cleanup](#let-the-operator-perform-cleanup).
- **Resources from any flavor or platform import** you kept in the cluster
  definition.
- **Kix's own CustomResourceDefinitions** (`activations.kix.run`,
  `packageinstances.kix.run`, `activationheads.kix.run`, and
  `packageinstancerevisions.kix.run`). Kix installs these CRDs automatically,
  and every Kix cluster on the same Kubernetes cluster shares them, so delete
  them only after removing the last Kix cluster.

Delete the remaining records, Namespaces, and CustomResourceDefinitions you no
longer need with `kubectl delete`.

:::note[Configure the history window]
See [Configure Activation retention](/docs/how-to/operate-a-cluster/configure-activation-retention/)
for the retention rules and scheduling example.
:::

:::note[Reference]
See [`kix gc`](/docs/reference/cli/gc/) for all flags and mode behavior.
:::