Security
Kubernetes Secret Cleanup: Remove Old Credentials Without Outages
Kubernetes Secret cleanup is credential rotation work, not a hunt for small objects in etcd. A Secret can look abandoned because its name contains an old project code, while a Deployment still reads it through envFrom, a CronJob mounts it only once a month, or an external controller refreshes it from a vault path.
The useful output is a rotation-and-removal record: which workloads read the Secret, which external account or certificate it represents, what replacement was deployed, and when the old value can be removed. That matters because the cleanup can still go wrong when a pod keeps running with an old environment variable until its next restart.
Key takeaways
- Treat each cleanup candidate as an owned system with dependencies, not anonymous clutter.
- Check pod specs, Helm values, ExternalSecret or SealedSecret sources, and recent rollout history before deciding that “quiet” means “unused.”
- Prefer reversible changes first when revoking credentials before rotation is complete is still plausible.
- Leave behind a rotation record with the new Secret name, old Secret removal date, owning service, and watch signal so the next review starts with context.
- Measure the result as lower spend, lower risk, less operational drag, or clearer ownership.
Map the Workload Boundary
Start with one namespace or application family and trace each Secret from manifest to runtime consumer to upstream credential. The review is small enough for service owners to answer, but complete enough to catch image pull secrets, init containers, and CronJobs.
| Field | Why it matters |
|---|---|
| Owner | Cleanup needs a person or team that can accept the decision |
| Current purpose | A short reason to keep the item, written in present tense |
| Last meaningful use | Pod mounts, environment references, rollout timestamps, external secret syncs, and credential audit logs |
| Dependency evidence | Deployment specs, CronJobs, init containers, Helm charts, GitOps manifests, and identity provider records |
| Risk if wrong | The outage, data loss, access failure, or rollback gap the review must avoid |
| Next action | Keep, reduce, archive, disable, remove, or investigate |
Do not make the inventory larger than the decision. A short list with owners and evidence beats a perfect spreadsheet that nobody is willing to act on.
Cluster Evidence to Trust
The useful question is not “how old is it?” It is “what would break, become harder to recover, or lose accountability if this disappeared?” For Kubernetes secret cleanup, collect enough evidence to answer that without relying on naming conventions.
| Check | What to look for | Cleanup signal |
|---|---|---|
| Pod consumption | secretKeyRef, envFrom, projected volumes, image pull secrets, and CSI mounts | No live or pending workload references the Secret |
| Source of truth | ExternalSecret, SealedSecret, SOPS file, vault path, or manual object | The source has already moved to a replacement |
| Credential audit | Last authentication for the API key, database user, certificate, or token inside the Secret | No caller uses the old credential after rotation |
| Rollout timing | Deployments, StatefulSets, Jobs, and CronJobs that need a restart to pick up new values | Every consumer has restarted or completed on the new value |
Use several signals together. Activity can miss monthly jobs and incident-only paths. Ownership can be stale. Cost can distract from security or recovery risk. The strongest case combines runtime data, dependency checks, owner review, and a rollback plan.
If the evidence conflicts, label the item “investigate” with a named owner and review date. That is still progress because the next review starts with a narrower question.
Example Evidence Check
Use a read-only query to find workloads that still mention a candidate Secret before rotating or deleting it.
kubectl get deploy,statefulset,daemonset,job,cronjob \
--all-namespaces -o yaml |
rg -n "secretName: $SECRET_NAME|secretKeyRef:|envFrom:"
The output proves only that manifests mention Secret fields near the candidate name. It does not prove whether the credential inside the Secret is still accepted by the upstream system.
Right-Size Before You Delete
Use the least permanent move that proves the decision. In Kubernetes secret cleanup, removal is only one possible outcome; reducing size, narrowing permission, shortening retention, archiving, or disabling a trigger may produce the same benefit with less risk.
- Create the replacement Secret first, then roll one workload and confirm the upstream credential logs show the new caller.
- Keep the old Secret during the rollback window if pods read values as environment variables.
- Remove image pull secrets separately from application secrets because the failure mode is different.
Track the cleanup candidate with a simple priority score:
| Score | Good sign | Bad sign |
|---|---|---|
| Impact | Meaningful spend, risk, toil, noise, or confusion disappears | The item is cheap and low-risk but politically distracting |
| Confidence | Owner, purpose, and dependency path are understood | The team is guessing from age or name |
| Reversibility | Restore, recreate, re-enable, or rollback path exists | Deletion would be the first real test |
| Prevention | A rule can stop recurrence | The same pattern will return next month |
Start with high-impact, high-confidence, reversible candidates. Defer confusing items only if they get an owner and a date; otherwise “defer” becomes another word for keeping waste permanently.
Kubernetes Cases That Need Patience
Some cleanup candidates are supposed to look quiet. Do not rush these cases:
- Secrets mounted by CronJobs that run monthly, quarter-close jobs, or certificate renewal hooks.
- Shared image pull secrets used by namespaces that do not share application ownership.
- Credentials synchronized from an external secret operator where the Kubernetes object is only a cache.
For these cases, use a longer observation window, explicit owner approval, and a staged reduction. The point is not to avoid cleanup; it is to avoid making the first proof of dependency an outage.
Run the Cluster Review
Run Kubernetes secret cleanup as a decision review, not an open-ended hygiene project.
- Pick the narrow scope and export the candidate list.
- Add owner, current purpose, last-use evidence, dependency checks, and risk if wrong.
- Remove obvious false positives, then ask owners to choose keep, reduce, archive, disable, remove, or investigate.
- Apply the least permanent useful change first.
- Watch the signals that would reveal a bad decision.
- Complete the final removal only after the review window closes.
- Save a rotation record with the old Secret, replacement Secret, upstream credential, consumers restarted, and revocation date.
For broader cleanup planning, use the cleanup library to pair this guide with related notes. If the cleanup has infrastructure impact, pair it with a visible owner, a rollback path, and a measurable business case. For infrastructure cleanup, the main cloud cost optimization checklist is a useful companion.
Stop Cluster Waste Returning
Prevention should change the creation path, not just the cleanup path. For Kubernetes secret cleanup, the useful prevention fields are owner, expiry date, least-privilege scope, rotation schedule, and removal notes. Make those fields part of normal creation and review.
- Require Secret manifests to include owner, upstream system, rotation cadence, and replacement procedure.
- Prefer one Secret per consuming service when shared credentials make impact analysis unclear.
- Put Secret rotation in the deployment path so removal follows a successful rollout instead of a separate cleanup project.
The recurring review should be short: sort by impact, pick the unclear items, assign owners, and close the loop on anything nobody claims. If the review keeps producing the same class of candidate, fix the creation path instead of celebrating repeated cleanup.
Example Decision Record
Use a compact record so the cleanup can be reviewed later without reconstructing the whole investigation.
| Field | Example entry for this cleanup |
|---|---|
| Candidate | Stale Kubernetes Secrets in Kubernetes clusters |
| Why it looked stale | Low recent activity, unclear owner, or no current consumer after the first review |
| Evidence checked | Pod references, external secret source, credential audit logs, and owner confirmation |
| First reversible move | Deploy replacement Secret and restart one low-risk consumer |
| Watch signal | The metric, alert, job, route, query, or owner complaint that would show the cleanup was wrong |
| Final action | Remove the old Secret after every consumer has restarted on the replacement and the old credential is revoked |
| Prevention rule | Require owner, source system, rotation cadence, and removal date on new Secrets |
This record is intentionally small. If the decision needs a long narrative, the candidate is probably not ready for removal yet. Keep investigating until the owner, evidence, reversible move, and prevention rule are clear.
FAQ
How often should teams do Kubernetes secret cleanup?
Match the cadence to credential risk and deployment frequency. High-scope production Secrets should be reviewed alongside rotation windows; low-risk namespace Secrets can be reviewed quarterly if every unclear item has an owner, source system, and next rotation date.
What is the safest first action?
The safest first action is to deploy the replacement Secret, move one low-risk consumer, and verify that the old credential stops receiving calls. That proves the rotation path before permanent removal.
What should not be removed quickly?
Do not rush Secrets shared across namespaces, synchronized by an external secrets operator, used by image pulls, or tied to rare CronJobs. Also slow down when revocation affects audit access, incident response, or a credential owned outside the cluster.
How do you make the decision useful later?
Write the decision as a small operational record: candidate, owner, evidence, chosen action, watch signals, rollback path, final date, and prevention rule. That format helps future engineers, search engines, and AI assistants understand the cleanup without guessing.