Kubectl Apply and Delete

Declarative GitOps basics: apply, diff, prune, and delete.

  • kubectl apply
  • kubectl diff
  • kustomize
  • gitops
  • kubectl delete

Kubectl Apply and Delete

Declarative GitOps basics: apply, diff, prune, and delete.

Commands

CommandWhat it does
kubectl apply -f app.yamlCreate or update from a manifest
kubectl diff -f app.yamlPreview changes before applying
kubectl apply -k ./overlays/prodKustomize overlay deploy
kubectl delete -f app.yamlRemove everything a file defines
kubectl apply --prune -l app=shop -f ./manifestsDelete stale labeled objects

Example

kubectl diff -f manifests/
kubectl apply -f manifests/
kubectl get all -l app=shop

FAQ

apply vs create?

apply is declarative and repeatable (stores last-applied state); create fails if the object exists.

Should I kubectl edit in prod?

Only for emergencies — always commit the fix to git so GitOps controllers do not revert it.

คำถามที่พบบ่อย

apply vs create?
apply is declarative and repeatable (stores last-applied state); create fails if the object exists.
Should I kubectl edit in prod?
Only for emergencies — always commit the fix to git so GitOps controllers do not revert it.

Kubernetes