apply
kubectl replace
Replaces a resource with the complete definition from a file, overwriting the entire spec. With --force it deletes and recreates the resource, which is useful for immutable fields.
kubectl replace -f FILENAME [--force] [flags] Common flags
| Flag | Purpose |
|---|---|
| -f, --filename | File containing the full replacement spec. |
| --force | Delete and recreate the resource (required for immutable fields). |
| --grace-period | Deletion grace period when --force is used. |
| --cascade | Cascade policy when --force triggers a delete. |
| --save-config | Record the applied config annotation for later kubectl apply. |
Examples
kubectl replace -f deploy.yaml Overwrite the Deployment spec with the file contents.
kubectl replace --force -f pvc.yaml Recreate a PVC to change an immutable field.
kubectl get deploy web -o yaml | kubectl replace -f - Round-trip edit via stdin.
Gotcha
replace requires the full object (including resourceVersion) and fails on conflicts; prefer apply for partial updates. --force performs a real delete, so PVCs bound to the resource may be released.
Related commands
kubectl apply
Declaratively creates or updates resources from a manifest file, directory, URL, or kustomization. Tracks the last-applied configuration so subsequent applies compute a three-way merge patch rather than replacing the object.
kubectl patch
Applies a partial update to a resource using strategic-merge, JSON-merge, or JSON-patch semantics. It is the surgical tool for changing a single field without rewriting the whole manifest.
kubectl edit
Fetches a live resource, opens it in your $EDITOR, and applies the diff on save. Convenient for one-off tweaks; not recommended for production changes that should live in Git.
kubectl create
Imperatively creates a resource from a file or from built-in generators (deployment, configmap, secret, job, etc.). Unlike apply, it fails if the resource already exists and does not record a last-applied annotation.