CodeBlanks Logo Icon CodeBlanks

kubectl delete

Learn to delete resources with kubectl delete.

kubectl delete

kubectl delete deletes objects by name, file or label:

kubectl delete pod web-7c5b9f6d8-abcde
kubectl delete -f web.yaml           # all objects in the file
kubectl delete deploy,svc -l app=web # those matching the label

Deleting a pod belonging to a deployment isn't permanent; the deployment creates a new one. To really remove the application, the deployment itself is deleted.

Graceful termination

A deleted pod is first sent SIGTERM and Kubernetes waits terminationGracePeriodSeconds (default 30 s). To remove a stuck pod immediately there is force deletion; but it doesn't guarantee the container really stopped, so it's used as a last resort:

kubectl delete pod web-abc --grace-period=60
kubectl delete pod web-abc --grace-period=0 --force   # last resort
kubectl delete pods --all                             # all pods in the namespace

Practice Cleanup

Write three commands: delete the objects in web.yaml; delete the deployments and services labelled app=api; delete the pod named eski-pod with a 5-second grace period.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl delete -f web.yaml
kubectl delete deploy,svc -l app=api
kubectl delete pod eski-pod --grace-period=5

Expected output

deployment.apps "web" deleted
deployment.apps "api" deleted
service "api" deleted
pod "eski-pod" deleted

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What happens if you delete a deployment's pod?

  • The deployment is deleted too
  • It errors
  • The deployment creates a new one in its place Correct answer
  • The application is removed completely

Explanation: The ReplicaSet keeps the desired replica count.

2 Delete the objects matching the label.

Question
kubectl delete deploy,svc ____ app=web
Answer
kubectl delete deploy,svc -l app=web

Explanation: -l (selector) selects by label.

3 Match.

Term Meaning
delete -f Delete what's in the file
delete -l Delete by label
--grace-period Termination time
--force Forced, last resort

All lessons in Intermediate