Sub topic 15
Deleting and pruning resources
This chapter assumes you can write a Deployment and a Service, apply them with kubectl apply -f, and use labels and selectors. It covers how to remove resources and why deleting a file does not delete the resource.
Removing a file does not remove the resource
kubectl apply only creates and updates. If you delete service.yaml from your directory and apply the directory again, the Service stays in the cluster. Kubernetes compares what you send with what exists, and an object you no longer mention is not a change.
You have to delete it explicitly, or use pruning.
Deleting resources
Three common forms:
# Delete everything named in a manifest file
kubectl delete -f service.yaml
# Delete by kind and name
kubectl delete deployment web -n demo
# Delete by label
kubectl delete pods,services -l app=web -n demoDeleting from the same file you applied is the safest habit, because you remove exactly what you created. Add --dry-run=server first to see what would be deleted without removing anything.
What gets deleted along with an object
Most objects own others through owner references. Deleting a Deployment also deletes its ReplicaSets and their Pods. This is called cascading deletion, and it is the default.
Two behaviors surprise people:
- Deleting a Pod that a Deployment owns does not remove it for good. The ReplicaSet notices the missing replica and creates a new Pod. Delete the Deployment instead.
- Deleting a Namespace deletes every resource inside it. Check the name before you run
kubectl delete namespace.
To remove a Deployment but keep its Pods running, use --cascade=orphan:
kubectl delete deployment web -n demo --cascade=orphanThe Pods become unowned and nothing replaces them if they fail.
Pruning with apply
Pruning lets apply delete objects that are in the cluster but no longer in your files. It is selected with a label:
kubectl apply -f manifests/ -n demo --prune -l app=web --dry-run=server
kubectl apply -f manifests/ -n demo --prune -l app=webKubernetes applies every manifest in the directory. Then it deletes objects in the namespace that carry app=web, are of a kind on the prune allowlist, and were not part of this apply.
Objects stuck in Terminating
A delete can hang when an object has a finalizer, which is a key in metadata.finalizers that tells a controller to do cleanup first. The object stays in Terminating until the controller finishes and removes the finalizer. Check the controller before you edit the finalizer by hand. Removing it early can leave external resources, such as cloud volumes, behind.
Choosing an approach
| Goal | Use |
|---|---|
| Remove what a file created | `kubectl delete -f file.yaml` |
| Remove one object | `kubectl delete <kind> <name>` |
| Keep a directory and the cluster in sync | `apply --prune -l <label>` |
| Remove an entire environment | Delete the Namespace |
Exercise
You delete service.yaml from your manifests directory and run kubectl apply -f manifests/. The Service web still exists. Why, and what are two ways to remove it?
Show answer
apply only creates and updates the objects in the files you give it, so a missing file is not treated as a delete. Remove the Service with kubectl delete service web -n demo (or kubectl delete -f on the old file). You can also run kubectl apply -f manifests/ -n demo --prune -l <label>, as long as the Service carries that label and you checked the result with --dry-run=server first.