Our clusters can accumulate old test namespaces, PVCs left behind by deleted StatefulSets, and ConfigMaps that may no longer be used. What approaches work for finding these and cleaning them up without deleting something that still matters?
How do you find and safely clean up forgotten Kubernetes resources?
Answers
Start with an inventory and make deletion a separate step. Labels or annotations for an owner and expiry date make it much easier to find someone who can confirm whether a namespace is still needed. A scheduled report can flag expired test namespaces for review.
For disposable test environments, you could eventually automate deletion after expiry. Keep that policy separate from production and make sure people know what the expiry means before relying on it.
Reply to this answer
Be especially careful with PVCs. A claim left after a StatefulSet was deleted might contain data someone still needs. Check its purpose, backups, and the associated PV before removing it.
Also check the PV reclaim policy. With Retain, deleting the claim leaves the PV and its backing storage behind, potentially still incurring charges. Released PVs are worth reviewing, but that state alone is not proof that the data can be discarded.
Reply to this answer
GitOps helps stop the pile from growing. Manage resources through a repository and decide whether the controller should prune objects removed from it. Some teams leave pruning as a manual approval step, especially in production.
It won't magically identify an old project that nobody has removed from the repository, though. And you'll still need a sweep for resources created before the current process was in place.
Reply to this answer
For discovery, a cluster inspection tool such as Popeye can provide checks, and a resource visualization tool can help show relationships between objects. Use those results as leads, not a delete list.
ConfigMaps are a good example of why. An apparent lack of references is worth investigating, but check how the application and its deployment process use the configuration before removing it.
Reply to this answer
If a cluster is fully reproducible, rebuilding it from declared configuration can clear out old manual artifacts. That's a bigger operation than a sweep, though, and persistent data needs its own migration or retention plan. It is not a shortcut for deciding whether a volume can be lost.
Reply to this answer
Add your answer
Answers are reviewed before publication.
_u47~tavzelo.9:
StatefulSets can also be configured to delete their PVCs when the StatefulSet is deleted. That can be useful for disposable workloads, but it is a risky default for data you need to keep.