Skip to content
LatchkeyLatchkey home

kubectl delete -l: Usage, Options & Common CI Errors

Delete every resource carrying a given label in one command.

kubectl delete -l removes all resources of a type that match a label selector - the idiom for tearing down everything a CI run created when you tagged it consistently. Scoping the selector tightly is what keeps it safe.

What it does

kubectl delete TYPE -l key=value deletes every matching object of that type. Combine selectors with commas for AND (run=ci,pr=42). It honours graceful termination like any delete, and -n scopes it to a namespace; without -n it acts on the current namespace only.

Common usage

Terminal
kubectl delete pods -l app=web
kubectl delete all -l run=ci,pr=${PR_NUMBER}
kubectl delete deploy,svc,cm -l release=preview-${PR_NUMBER}
kubectl delete pods -l app=web --dry-run=client       # preview the match

Common errors in CI

The dangerous mistake is an over-broad or empty selector: a typo that drops the selector entirely turns delete pods -l '' into delete pods (everything in the namespace), so always --dry-run=client the selector first and scope teardown to a dedicated namespace you can simply delete instead. delete all -l does NOT cover every kind - "all" is a curated set (pods, services, deployments, etc.) and misses ConfigMaps, Secrets, PVCs, and CRDs, so a namespace can look empty while those linger. A selector matching nothing prints "No resources found" and exits 0, which can mask a labelling bug.

Using this in CI

A runner has no kubeconfig, no cached context, and no interactive auth. Every kubectl invocation in CI needs the context supplied explicitly, and most confusing CI failures here are the command running against the wrong cluster or no cluster at all.

Terminal
# never rely on the ambient context on a runner
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get pods

# confirm what you are actually connected to before mutating anything
kubectl config current-context
kubectl cluster-info

# fail fast instead of hanging on an unreachable API server
kubectl --request-timeout=30s get nodes

Frequently asked questions

kubectl delete -l: Usage, Options & Common CI Errors?
kubectl delete -l removes all resources of a type that match a label selector - the idiom for tearing down everything a CI run created when you tagged it consistently. Scoping the selector tightly is what keeps it safe.
What it does?
kubectl delete TYPE -l key=value deletes every matching object of that type. Combine selectors with commas for AND (run=ci,pr=42). It honours graceful termination like any delete, and -n scopes it to a namespace; without -n it acts on the current namespace only.
Common errors in CI?
The dangerous mistake is an over-broad or empty selector: a typo that drops the selector entirely turns delete pods -l '' into delete pods (everything in the namespace), so always --dry-run=client the selector first and scope teardown to a dedicated namespace you can simply delete instead.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card