kubectl delete: Removing Kubernetes Resources
The kubectl delete command is used to remove resources from your Kubernetes cluster. It’s a powerful and irreversible operation, so always use it with caution and ensure you understand what you are deleting.
Basic Deletion Commands
Deleting a Single Resource by Name
The most common way to delete a resource is by specifying its type and name.
# Delete a specific pod
kubectl delete pod <pod-name> -n <namespace>
# Delete a specific deployment
kubectl delete deployment <deployment-name> -n <namespace>
# Delete a service
kubectl delete service <service-name> -n <namespace>Deleting Resources from a File
If you created resources using a YAML file, you can delete them using the same file.
kubectl delete -f <your-resource-file.yaml>Deleting Resources by Label
You can delete multiple resources that share a common label.
# Delete all pods with the label 'app=myapp' in a specific namespace
kubectl delete pods -l app=myapp -n <namespace>
# Delete deployments and services with the label 'environment=dev' across all namespaces
kubectl delete deployment,service -l environment=dev --all-namespacesAdvanced: Bulk Deletion Based on Labels (with Caution)
The following complex command uses kubectl get, jq, and bash to dynamically identify and delete resources based on specific labels. This is a very powerful command that should be used with extreme care, as it performs immediate deletion without a confirmation prompt.
WARNING: This command will immediately delete any deployment, service, persistentvolumeclaim, persistentvolume, and ingress resources across all namespaces that match the label app=unify. There is no undo.
kubectl get deploy,svc,pvc,pv,ing --all-namespaces -l app=unify -o json | \
jq -r '.items[] | "kubectl delete \(.kind | ascii_downcase) \(.metadata.name) -n \(.metadata.namespace // "default")"' | \
bashCommand Breakdown:
kubectl get deploy,svc,pvc,pv,ing --all-namespaces -l app=unify -o json:- Retrieves
deployment,service,pvc,pv, andingressresources. --all-namespaces: Searches across all namespaces.-l app=unify: Filters resources by the labelapp=unify.-o json: Outputs the results in JSON format.
- Retrieves
jq -r '.items[] | "kubectl delete \(.kind | ascii_downcase) \(.metadata.name) -n \(.metadata.namespace // "default")"':- Pipes the JSON output to
jq, a JSON processor. - It iterates through each item (
.items[]) in the JSON output. - For each item, it constructs a
kubectl deletecommand string using the resource’skind(converted to lowercase),name, andnamespace. If a namespace is not present (e.g., for Cluster-scoped resources likePersistentVolume), it defaults to"default". -r: Raw output, sojqdoesn’t add extra quoting.
- Pipes the JSON output to
| bash: Pipes each constructedkubectl deletecommand string directly tobashfor execution. This is why the command is so dangerous.
Use this advanced command only when you are absolutely certain of its impact. For most situations, safer and more controlled deletion methods are recommended.