Error from server (AlreadyExists): error when creating "deploy.yaml": deployments.apps "web" already exists (Kubernetes) — how to fix
Error from server (AlreadyExists): error when creating "deploy.yaml": deployments.apps "web" already exists
Error from server (AlreadyExists): namespaces "dev" already exists
Quick fix: Use kubectl apply -f deploy.yaml to create-or-update instead of kubectl create. For imperative commands, pipe a dry run into apply: kubectl create namespace dev --dry-run=client -o yaml | kubectl apply -f -. To start over, kubectl delete -f deploy.yaml first.
What it means
Every Kubernetes object is identified by its kind, namespace and name. kubectl create sends a create request; if that identity is already taken, the API server answers with AlreadyExists (HTTP 409). It's common when re-running a setup script or CI job.
Check what's there:
kubectl get deployment web -n default -o wide
kubectl get all -n dev
Fix 1: use kubectl apply
apply creates the object if missing and updates it if it exists, so it's safe to run repeatedly:
kubectl apply -f deploy.yaml
kubectl apply -f k8s/ # whole directory
If the object was originally made with kubectl create, the first apply prints a warning that the kubectl.kubernetes.io/last-applied-configuration annotation is missing and patches it in. That's expected; later applies are clean.
Fix 2: idempotent imperative commands
kubectl create namespace, create secret, create configmap have no "if not exists" flag. Generate the YAML and apply it instead:
kubectl create namespace dev --dry-run=client -o yaml | kubectl apply -f -
kubectl create secret generic db-creds \
--from-literal=password=s3cret \
--dry-run=client -o yaml | kubectl apply -f -
kubectl create configmap app-config --from-file=config.yaml \
--dry-run=client -o yaml | kubectl apply -f -
This also updates the Secret/ConfigMap when the content changes, which plain create never does.
Fix 3: replace or recreate
To overwrite the object completely with the file:
kubectl replace -f deploy.yaml
To delete it and create it fresh (causes downtime for workloads):
kubectl delete -f deploy.yaml
kubectl create -f deploy.yaml
# or in one step:
kubectl replace --force -f deploy.yaml
Some changes can't be applied in place (for example a Deployment's spec.selector) and fail with "field is immutable"; recreating is the way out there.
Fix 4: the object is being deleted
If you just deleted it, it may still exist while finalizers run, and creating it again returns AlreadyExists. Check:
kubectl get namespace dev -o jsonpath='{.status.phase}' # Terminating?
kubectl get pvc data -o jsonpath='{.metadata.finalizers}'
Wait for deletion to finish (kubectl wait --for=delete namespace/dev --timeout=120s) before re-creating.
Fix 5: wrong namespace or name collision
Two manifests may use the same name, or a metadata.namespace in the file differs from what you expect. Make names unique per namespace and check with:
kubectl apply -f deploy.yaml --dry-run=server
Helm: "rendered manifests contain a resource that already exists"
Helm reports an existing object it doesn't own as Unable to continue with install: ... exists and cannot be imported into the current release: invalid ownership metadata. Either delete the object, install with a different name, or let Helm adopt it by adding its ownership metadata:
kubectl label deployment web app.kubernetes.io/managed-by=Helm
kubectl annotate deployment web meta.helm.sh/release-name=myrelease meta.helm.sh/release-namespace=default
Validate manifests before applying with the YAML validator.
Last updated 2026-11-29.