kubectl delete command is used to remove resources from a Kubernetes cluster by specifying the resource name, resource type, configuration file, or label selector.
- It deletes individual resources or multiple resources of a specified type.
- It supports YAML and JSON configuration files.
- It can delete resources matching a label selector.
- It supports graceful deletion and force deletion for supported resources.
Syntax
The general syntax of the kubectl delete command is:
kubectl delete ([-f FILENAME] | TYPE [(NAME | --all)])-f FILENAMEspecifies a YAML or JSON file containing resource definitions.TYPEspecifies the resource type, such aspod,deployment, orservice.NAMEspecifies the name of the resource to delete.--allselects all resources of the specified type within the applicable namespace.-n NAMESPACEspecifies the namespace in which to delete resources.-Aor--all-namespacesapplies the command across namespaces where supported.
Creating Kubernetes Resources
Consider a Kubernetes cluster running an Nginx Deployment and a Service. You can use these resources to understand how kubectl delete works. Create a file named nginx-deployment.yaml with the following configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Apply the configuration to create the Deployment.
kubectl apply -f nginx-deployment.yamlCreate a Service using the following configuration in nginx-service.yaml:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
Apply the Service configuration.
kubectl apply -f nginx-service.yamlCheck the resources in the default namespace.
kubectl get all
This command displays common resource types, including Pods, Deployments, ReplicaSets, and Services.
Deleting a Deployment
A Deployment manages ReplicaSets and Pods for an application. You can delete a Deployment by specifying its name or using its configuration file. To delete a Deployment named nginx-deployment, run:
kubectl delete deployment nginx-deployment
Alternatively, delete the Deployment using its configuration file.
kubectl delete -f nginx-deployment.yaml- Deleting the Deployment also removes its dependent ReplicaSet and managed Pods by default.
- The command does not delete a separately created Service.
- You can verify the deletion using
kubectl get deployments.
Deleting a Service
A Service provides network access to a group of Pods. You can remove it by specifying its name or configuration file.
To delete the Service named nginx-service, run:
kubectl delete service nginx-service\
Alternatively, use the Service configuration file.
kubectl delete -f nginx-service.yaml- Deleting a Service removes that Service resource from the cluster.
- The Pods and Deployment associated with it continue running unless they are deleted separately.
- You can verify the deletion using
kubectl get services.
Deleting a Pod
A Pod is the smallest deployable unit in Kubernetes. You can delete an individual Pod by specifying its name.
kubectl delete pod pod-name
Replace pod-name with the actual name of the Pod.
- Kubernetes normally allows a Pod to terminate gracefully before removing it.
- A Pod managed by a Deployment or another controller may be recreated automatically.
- You can check the remaining Pods using
kubectl get pods.
Delete Resources Using a Configuration File
The -f flag allows you to delete resources defined in a YAML or JSON file. This is useful when resources were created using the same configuration file.
Run the following command to delete the resources defined in a file:
kubectl delete -f nginx-deployment.yamlTo delete resources defined in multiple files, specify each file separately.
kubectl delete -f nginx-deployment.yaml -f nginx-service.yaml- The command deletes the resources described in the specified files.
- The files must contain the resource definitions that you intend to remove.
- Deleting resources from a file does not delete unrelated resources in the cluster.
Delete All Pods in One Namespace
You can delete all Pods within a particular namespace by combining the --all and -n flags.
kubectl delete pods --all -n your-namespace
Replace your-namespace with the name of the target namespace.
- The command selects all Pods in the specified namespace.
- It does not delete Deployments, Services, or other resource types directly.
- Pods managed by controllers may be recreated after deletion.
Delete All Pods Across All Namespaces
You can use the -A flag to target Pods across all namespaces. Combining it with --all selects all Pods of that type.
kubectl delete pods --all -A
- The command targets Pods across namespaces, including system namespaces.
- Pods managed by Deployments or other controllers may be recreated.
- Deleting system Pods can disrupt cluster components, so review the targets before running the command.
Delete Pods in Multiple Namespaces
The kubectl delete command does not accept multiple -n flags as a way to select several namespaces in one invocation. You can run a separate command for each namespace.
For example, to delete all Pods in two namespaces, run:
kubectl delete pods --all -n developmentkubectl delete pods --all -n testing
- Replace
developmentandtestingwith the actual namespace names. - Each command affects only the namespace specified in that command.
- Review the target namespaces before executing the commands.
Delete Resources Using Labels
Kubernetes labels help identify related resources. The -l or --selector flag allows you to delete resources that match a specified label.
For example, delete all Pods with the label app=nginx in the current namespace.
kubectl delete pods -l app=nginxTo delete matching Pods in a specific namespace, run:
kubectl delete pods -l app=nginx -n your-namespace- Only resources matching the specified selector are targeted.
- Check the labels using
kubectl get pods --show-labelsbefore deletion. - If the Pods are managed by a controller, replacement Pods may be created.
Force Delete Pods
Kubernetes normally gives Pods time to terminate gracefully. Force deletion removes the Pod object from the API server without waiting for confirmation that the processes have stopped on the node.
To force-delete a specific Pod, run:
kubectl delete pod pod-name --grace-period=0 --forceTo force-delete all Pods in a particular namespace, run:
kubectl delete pods --all --grace-period=0 --force -n your-namespace--grace-period=0specifies a zero-second termination grace period.--forcerequests immediate removal of the resource from the API server.- Force deletion can cause inconsistencies if the workload is still running on a node.
Use force deletion only when necessary, particularly when dealing with Pods on unreachable nodes.
Delete Common Resources in One Namespace
The kubectl delete all --all command removes common resource types selected by the all resource shortcut. It does not literally delete every Kubernetes resource.
To delete the common resource types in a specific namespace, run:
kubectl delete all --all -n your-namespace- The command targets common resources such as Pods, Services, Deployments, and ReplicaSets.
- It does not necessarily remove ConfigMaps, Secrets, PersistentVolumeClaims, or other resource types.
- Review the resources in the namespace before running the command.
Delete Common Resources Across All Namespaces
You can apply the same command across namespaces by adding the -A flag.
kubectl delete all --all -A- The command targets common resource types across namespaces.
- It does not remove every resource type or delete the namespaces themselves.
- Removing application resources can disrupt running workloads, so use this command only when you understand its impact.
Verify Resource Deletion
After executing a deletion command, check the cluster to confirm whether the targeted resources have been removed.
- Run
kubectl get podsto check Pods in the current namespace. - Run
kubectl get deploymentsto check Deployments. - Run
kubectl get servicesto check Services. - Add
-n your-namespaceto inspect a specific namespace. - Add
-Ato list the relevant resource type across namespaces.
A successful deletion request does not always mean that every resource disappears immediately. Graceful termination, finalizers, and controller behavior can affect the result.