Kubernetes makes it easy to create many resources at once, with the kubectl apply -f filename.yaml
command creating all the resources in a compound YAML file. But how do you then delete multiple resources without specifying them individually?
In this post, I show you how to perform bulk deletions of Kubernetes resources.
Read our white paper: A leader's guide to Kubernetes CD at scale
Learn how to overcome technical hurdles and business challenges and successfully implement Kubernetes.
Get the white paperExample Kubernetes deployment
Let's take a look at a typical YAML file describing a Kubernetes deployment and service:
apiVersion: v1
kind: Service
metadata:
name: my-nginx-svc
labels:
app: nginx
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
When this YAML is saved to a file called nginx.yaml
, the resources are created with the following command:
kubectl apply -f nginx.yaml
You can then view the new resources created with the following commands:
kubectl get pods
kubectl get deployments
kubectl get services
You see three pods, one deployment, and one service are created. The pods aren't directly defined in the YAML file and are created by the deployment, with three pods created due to the replicas
property being set to 3
.
Deleting resources from file
The easiest way to delete these resources is to use the delete
command and pass the same file that was used to initially create the resources:
kubectl delete -f nginx.yaml
If you re-run the kubectl get
commands above you see the pods, deployment, and service are deleted. Because the pods are managed by the deployment, deleting the deployment also deletes the pods.
Manually deleting resources
To manually delete specific types of resources, the kubectl delete
command accepts an --all
argument that defines the type of resource to delete. For example, the following command deletes all the services:
kubectl delete --all services
You can confirm the services are deleted with the command:
kubectl get services
This command deletes all the pods:
kubectl delete --all pods
The output of the command looks something like this:
pod "my-nginx-6595874d85-88jlr" deleted
pod "my-nginx-6595874d85-9w52c" deleted
pod "my-nginx-6595874d85-dpzds" deleted
However, something interesting happens when you confirm the pods are deleted. Run the following command to list any pods:
kubectl get pods
Notice that there are still 3 pods, with the output looking something like this:
NAME READY STATUS RESTARTS AGE
my-nginx-6595874d85-2j4g8 1/1 Running 0 76s
my-nginx-6595874d85-4vrfb 1/1 Running 0 76s
my-nginx-6595874d85-4wj9p 1/1 Running 0 76s
If you look closely, the pod names shown by the kubectl get pods
command are different to those returned by the kubectl delete --all pods
command. This is because the pods are managed by the deployment, and when the deployment sees that the pods it was managing have been deleted, it recreates new pods to fulfill its replica
count.
Deleting pods managed by a deployment essentially recreates them, which is useful if you want to force the pods to restart. But the only way to permanently delete the pods is to delete their parent deployment. This is done with the command:
kubectl delete --all deployments
After the deployment is deleted, there's no deployments or pods.
Deleting namespaces
Namespaces are a convenient way to group related resources. Create a new namespace called foo
with the command:
kubectl create namespace foo
Then create the NGINX resources in the new namespace with the command:
kubectl apply -f nginx.yaml -n foo
List the resources with the commands:
kubectl get pods -n foo
kubectl get deployments -n foo
kubectl get services -n foo
Then delete the namespace with the command:
kubectl delete namespace foo
This results in the namespace, and all the resources contained in it, being deleted.
Shorthand "all" resource
You can pass all
for the resource type when calling kubectl
to reference a common subset of Kubernetes resource types. So the following command deletes the service, deployment, and pods:
kubectl delete all --all
The all
type includes:
- pod
- service
- daemonset
- deployment
- replicaset
- statefulset
- job
- cronjobs
Deleting resources matching a label
Labels are used to enrich resources with metadata often describing things like the resource's purpose, environment, and version. You can select resources based on these labels to delete them. This lets you selectively delete groups of resources.
The following command deletes deployments with the label called app
set to nginx
:
kubectl delete deployments -l app=nginx
Likewise, you can delete the services with the same label:
kubectl delete service -l app=nginx
Dry runs
Bulk deletion of resources is convenient, but dangerous. Fortunately, kubectl
has the --dry-run
argument that lets you see the resources that would be deleted, but without actually deleting them. The following command previews the resources that would be matched by the all
resource type:
kubectl delete all --all --dry-run
Conclusion
Bulk deleting resources is easy with kubectl
, and in this post you learned how to delete resources:
- Defined in a YAML file
- Matching a single resource type
- Grouped in the
all
resource type - Contained in a namespace
- With matching labels
You also learned how to use the --dry-run
argument to preview any resources that would be deleted.
Happy deployments!