You need to delete a deployment in Kubernetes to free up resources, update or reconfigure applications, manage testing environments, scale down during low usage, or troubleshoot issues. This ensures your cluster remains efficient, clean, and up-to-date. You can use the kubectl delete deployment command to delete deployments in Kubernetes.
This guide will explain various methods to delete Kubernetes deployments with practical examples.
Table of Contents
Basic Syntax of kubectl delete deployment
The kubectl delete deployment command is used to delete a deployment resource in Kubernetes. Here is the basic syntax:
# kubectl delete deployment deployment_name
Replace deployment_name with the name of the deployment you want to delete.
Deleting a Single Deployment
The most straightforward way to delete a deployment is by specifying its name. Let’s say we have a deployment named my-app. To delete this deployment, use the following command:
# kubectl delete deployment my-app
The above command deletes a deployment only in a default namespace.
To verify that the deployment has been deleted, you can list all deployments and check that my-app is no longer present:
# kubectl get deployments
Output.
No resources found in default namespace.
Deleting Multiple Deployments
You can delete multiple deployments by listing their names separated by spaces. For example, if you have two deployments named app1 and app2, you can delete them with a single command:
# kubectl delete deployment app1 app2
To verify that both deployments have been deleted, list all deployments:
# kubectl get deployments
Deleting Deployments Using Label Selectors
Sometimes, you may want to delete deployments based on specific labels. This can be useful when you have multiple deployments with a common label.
For example, to delete all deployments with the label env=staging, use the following command:
# kubectl delete deployment -l env=staging
Output.
deployment.apps "staging-app1" deleted
deployment.apps "staging-app2" deleted
To verify that the deployments with the label env=staging have been deleted, run:
# kubectl get deployments -l env=staging
Deleting Multiple Deployments Using Label Selectors
If you have many deployments then you can use the label selectors to delete all deployments based on labels.
For example, if you have deployments labeled with app=web and you want to delete all of them, use:
# kubectl delete deployment -l app=web
Output.
deployment.apps "web-app1" deleted
deployment.apps "web-app2" deleted
deployment.apps "web-app3" deleted
To verify that the deployments with the label app=web have been deleted, run:
# kubectl get deployments -l app=web
Cascading Delete
By default, deleting a deployment also deletes its associated ReplicaSets and Pods. This is known as a cascading delete. You can explicitly specify this behavior using the –cascade flag.
For example, to delete a deployment named my-app and ensure all associated resources are also deleted, use:
# kubectl delete deployment my-app --cascade=true
Output.
deployment.apps "my-app" deleted
To verify that the deployment and its associated resources have been deleted, list all ReplicaSets and Pods:
# kubectl get rs && kubectl get pods
Output.
No resources found in default namespace.
Force Deletion
In some cases, you may need to delete a deployment forcefully. This can be done using the –force and –grace-period=0 flags.
For example, to force delete a deployment named my-app, use:
# kubectl delete deployment my-app --force --grace-period=0
Output.
deployment.apps "my-app" force deleted
Deleting Deployments in a Specific Namespace
If you have deployments in different namespaces, you can specify the namespace while deleting.
For example, to delete a deployment named my-app in the production namespace, use:
# kubectl delete deployment my-app -n production
To verify that the deployment has been deleted from the production namespace, list all deployments in that namespace:
# kubectl get deployments -n production
Deleting a Deployment Using YAML Configuration File
Another method to delete a deployment is by using a YAML configuration file.
If you have a deployment configuration file named my-app-deployment.yaml, you can delete it using:
# kubectl delete -f my-app-deployment.yaml
Deleting All Deployments in the Default Namespace
To delete all deployments in the default namespace, you can use the –all flag. This is useful when you want to clean up all deployments without specifying each one by name:
# kubectl delete deployments --all
Output.
deployment.apps "app1" deleted
deployment.apps "app2" deleted
deployment.apps "app3" deleted
Deleting All Deployments in All Namespaces
If you need to delete all deployments across all namespaces, you can combine the –all flag with the –all-namespaces flag. This command will delete every deployment in your Kubernetes cluster:
# kubectl delete deployments --all --all-namespaces
Output.
deployment.apps "app1" deleted from namespace "default"
deployment.apps "app2" deleted from namespace "production"
deployment.apps "app3" deleted from namespace "development"
To verify that all deployments have been deleted from all namespaces, list all deployments in all namespaces:
# kubectl get deployments --all-namespaces
Conclusion
In this article, we explored various methods to use the kubectl delete deployment command in Kubernetes. We also covered advanced methods such as deleting deployments using YAML configuration files and deleting all deployments in specific or all namespaces.
FAQs
1. What happens when you delete a Kubernetes deployment?
Deleting a deployment removes all associated pods, replica sets, and the deployment itself from the cluster.
2. Can I delete a deployment but keep its pods running?
No, deleting a deployment will also delete its managed pods. To keep the pods running, consider scaling the deployment to zero replicas instead.
3. How do I force delete a stuck deployment?
To force delete a deployment that is stuck, run: kubectl delete deployment deployment_name --grace-period=0 --force
4. How do I rollback a deleted deployment?
Once a deployment is deleted, you cannot rollback unless you have a backup or version control of your YAML file to re-create it.