When you use Kustomize’sDocumentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
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: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 yourkustomization.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:Quick Reference
| Action | Command / Config Snippet |
|---|---|
| Label generators | options.labels.app-config: my-config |
| Apply with pruning | kubectl apply -k <overlay> --prune -l app-config=my-config |
| List ConfigMaps post-prune | kubectl get configmap |