kubectl "Error from server (NotFound)" - Fix Missing Objects in CI
kubectl asked the API server to operate on a named object that does not exist in the targeted namespace. The resource type is known - this specific instance is simply not there.
What this error means
kubectl get/delete/patch/rollout fails with Error from server (NotFound): <kind> "<name>" not found. Unlike "no resource type", the kind is valid; the named object is missing or you are looking in the wrong namespace.
Error from server (NotFound): deployments.apps "api" not foundCommon causes
Wrong namespace
The object lives in another namespace, but the command ran without -n (defaulting to default) or against the wrong one. Namespaced lookups only see the targeted namespace.
Object never created or already deleted
A delete/patch step assumes the object exists, but a prior step never created it (or already removed it). Misordered CI steps and re-runs cause this.
How to fix it
Confirm the namespace and the object
List across namespaces to find where (and whether) it exists.
kubectl get deploy api -A
kubectl get deploy -n <ns>Make destructive steps tolerate absence
- Target the correct namespace explicitly with
-non every command. - For idempotent cleanup, add
--ignore-not-found=trueto deletes. - Order CI so the object is created before any step references it.
kubectl delete deploy api -n prod --ignore-not-found=trueHow to prevent it
- Pass
-n <namespace>explicitly; never rely on the default namespace in CI. - Use
--ignore-not-found=truefor cleanup steps that may run before creation. - Sequence create-before-use across pipeline stages.