Skip to main content
When you use Kustomize’s configMapGenerator or secretGenerator, each modification produces a brand-new ConfigMap or Secret. Over time, this leads to multiple stale objects cluttering your cluster. This guide shows how to label and prune unused resources to keep your namespace clean.

The Problem: Stale ConfigMaps & Secrets

Run the following command to list all ConfigMaps:
You might see output like this:
Here, you can spot three versions each of db-cred and redis-cred, but only the most recent ones are actually in use. The rest are stale leftovers.

Solution Overview: kubectl apply --prune

By adding a shared label to all generated ConfigMaps/Secrets and running kubectl apply --prune, Kubernetes will automatically delete any resource with that label that is no longer part of your current build.

Step 1: Add a Common Label

In your kustomization.yaml, include identical labels under options for each generator:

Step 2: Update Your Generator

When you change a literal—e.g., updating the password—keep the same label:

Step 3: Apply with Prune

Use the --prune flag along with -l to target your shared label:
kubectl apply --prune will delete any resource in the namespace matching the label selector that isn’t in the current Kustomize output. Make sure only intended resources use this label.

Step 4: Verify Cleanup

After pruning, run:
You should now see only the active ConfigMaps:

Quick Reference

Further Reading

Watch Video