Sign In

Curriculum 9: Deleting Resources

kubectl delete

12 min · 15 XP

kubectl delete -- Removing Resources

kubectl delete removes resources from your cluster. It supports deleting by name, label, file, and namespace, giving you fine-grained control over what gets removed.

Deleting by Name

The most straightforward approach -- specify the resource type and name:

# Delete a single pod
kubectl delete pod my-app-7d4b8c6f5-abc12

# Delete a deployment
kubectl delete deployment my-app

# Delete a service
kubectl delete service my-service

# Delete multiple resources of the same type
kubectl delete pod pod-1 pod-2 pod-3

Deleting by Label

Remove all resources matching a label selector:

# Delete all pods with a specific label
kubectl delete pods -l app=old-app

# Delete all resources with a label across types
kubectl delete deployment,service,configmap -l app=old-app

This is useful for cleaning up all resources that belong to a specific application.

Deleting by File

Delete the exact resources defined in a manifest file:

# Delete resources defined in a file
kubectl delete -f deployment.yaml

# Delete resources from multiple files
kubectl delete -f deployment.yaml -f service.yaml

# Delete all resources in a directory
kubectl delete -f ./manifests/

This is the declarative counterpart to kubectl apply -f and is especially useful for clean teardowns.

Deleting Everything in a Namespace

Remove all resources of a type in a namespace:

# Delete all pods in the current namespace
kubectl delete pods --all

# Delete all pods in a specific namespace
kubectl delete pods --all -n staging

# Delete all resources of multiple types
kubectl delete deployment,service,configmap --all -n staging

To delete an entire namespace and everything in it:

kubectl delete namespace staging

Be careful -- deleting a namespace removes every resource inside it and cannot be undone.

Waiting for Deletion

Use --wait to block until the resource is fully deleted:

kubectl delete pod my-app --wait=true

By default, kubectl delete waits for the resource to be removed. Use --wait=false to return immediately without waiting:

kubectl delete pod my-app --wait=false

Key Takeaways

  • Delete by name for targeted removal of specific resources
  • Delete by label with -l to clean up groups of related resources
  • Delete by file with -f for declarative teardowns
  • --all removes all resources of a type in a namespace
  • Deleting a namespace removes everything inside it -- use with caution