kubectl delete: Command Reference for CI/CD
Remove resources by name, label, or the manifest you applied.
kubectl delete powers teardown jobs and preview-environment cleanup. This reference lists the flags that control scope and termination timing, plus a CI pattern that scopes deletes to a namespace so a stray --all cannot wipe the wrong place.
Common flags and usage
- -f <manifest>: delete exactly what a manifest set created
- -l, --selector: delete every object matching a label
- --all: delete all objects of a type in the namespace (scope with -n!)
- --wait=false: return without blocking on graceful termination
- --grace-period=0 --force: skip graceful termination for a stuck pod
- --timeout: cap how long delete blocks before failing
Example
shell
# Tear down a per-build preview namespace
kubectl delete namespace ci-${BUILD_ID} --timeout=120s
# Targeted teardown of just this build's objects
kubectl delete -f k8s/ -l build=${BUILD_ID} --wait=trueIn CI
Always scope teardown to a dedicated namespace and delete that namespace rather than using --all without -n, which deletes everything in the default namespace. Pass --timeout so a finalizer that hangs a delete fails the step instead of stalling until the job-level timeout.
Key takeaways
- Scope teardown to a namespace; --all without -n hits the default namespace.
- A delete that hangs is usually a finalizer; cap it with --timeout.
- --grace-period=0 --force is a last resort for known-stuck fixtures only.
Related guides
kubectl Cheat Sheet: Commands for Everyday KubernetesA kubectl cheat sheet - get, describe, logs, apply, rollout, debug, and context commands for daily Kubernetes…
kubectl get: Command Reference for CI/CDReference for kubectl get: list resources, output formats, label and field selectors, jsonpath extraction, an…
kubectl wait: Command Reference for CI/CDReference for kubectl wait: block on a condition (Ready, Available, complete, delete) with a timeout, the rig…
kubectl create: Command Reference for CI/CDReference for kubectl create: imperative resource creation, the create-then-apply pattern, --dry-run YAML gen…