kubectl apply and delete
Create, update, compare, and remove Kubernetes resources using both declarative manifests and imperative helper commands.
Declarative Resource Management
The most common Kubernetes workflow is to store manifests in files and use kubectl apply to create or update resources.
Apply a Manifest
kubectl apply -f deployment.yaml
kubectl apply -f k8s/
apply is powerful because it works for both creation and updates.
Delete a Manifest
kubectl delete -f deployment.yaml
kubectl delete -f k8s/
Deleting by file is useful when you want to remove the exact resources defined in a manifest set.
Delete by Resource Type and Name
kubectl delete pod my-pod
kubectl delete deployment web
This is handy for quick cleanup, but for team workflows, deleting from files is often clearer and safer.
Imperative Create Commands
kubectl create deployment web --image=nginx
kubectl expose deployment web --port=80 --target-port=80 --type=ClusterIP
These are great for fast demos or generating starting YAML.
Generate YAML Without Creating Anything
One of the most useful tricks is --dry-run=client -o yaml.
kubectl create deployment web --image=nginx --dry-run=client -o yaml
kubectl create namespace demo --dry-run=client -o yaml
This lets you quickly scaffold a manifest, then save and edit it.
Compare Changes Before Applying
kubectl diff -f deployment.yaml
kubectl diff shows the difference between your file and the live resource. That helps you catch accidental changes before they hit the cluster.
Recommended Workflow
kubectl create deployment web --image=nginx --dry-run=client -o yaml
kubectl diff -f deployment.yaml
kubectl apply -f deployment.yaml
kubectl delete -f deployment.yaml
Why apply Is Preferred
apply supports the declarative model: you describe where you want the system to end up.
Think of
applylike updating a blueprint rather than manually moving bricks one by one.
That mindset scales better in teams and production systems.
Dry Run Trick
Why is `--dry-run=client -o yaml` so useful?
Diff Benefit
What is the main value of `kubectl diff -f deployment.yaml`?