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

← All kubectl commands · Docker vs Kubernetes