How to Use Kubectl Delete Deployment (With Examples)

Photo by Growtika on Unsplash

How to Use Kubectl Delete Deployment (With Examples)

How to Delete a Deployment in Kubernetes

Kubectl is a powerful command-line tool used for managing Kubernetes clusters. It allows you to deploy, scale, and manage containerized applications on Kubernetes clusters. One of the common tasks that you may need to perform is deleting a deployment. In this article, we will discuss how to use kubectl delete deployment command and provide examples to help you understand the process.

What is Kubernetes?

Kubernetes is an open-source platform for managing containerized workloads and services. It provides a framework for automating the deployment, scaling, and management of containerized applications. Kubernetes has become the de facto standard for container orchestration and is widely used in modern cloud-native applications.

What is a deployment in Kubernetes?

A deployment in Kubernetes is a resource that manages a set of identical pods. It provides declarative updates for Pods and ReplicaSets. A deployment ensures that the desired number of replicas is running and can perform rolling updates to deploy new versions of your application. You can use kubectl to manage deployments and perform various operations such as creating, updating, scaling, and deleting deployments.

How to use kubectl delete deployment command?

The kubectl delete deployment command is used to delete a deployment in Kubernetes. You can delete a deployment by name, label selector, or using a manifest file. Here are the different ways to delete a deployment in Kubernetes:

Deleting a deployment by name

To delete a deployment by name, use the following command:

kubectl delete deployment <deployment-name>

Replace <deployment-name> with the name of the deployment you want to delete. For example, to delete a deployment named myapp, use the following command:

kubectl delete deployment myapp

Deleting a deployment by label selector

You can also delete a deployment by label selector. Use the following command to delete all deployments with a specific label:

kubectl delete deployment -l <label-selector>

Replace <label-selector> with the label selector for the deployments you want to delete. For example, to delete all deployments with the label app=myapp, use the following command:

kubectl delete deployment -l app=myapp

Deleting a deployment using a manifest file

You can also delete a deployment using a manifest file. Use the following command to delete a deployment using a manifest file:

kubectl delete -f <manifest-file>

Replace <manifest-file> with the path to the manifest file for the deployment you want to delete. For example, to delete a deployment using the manifest file myapp-deployment.yaml, use the following command:

kubectl delete -f myapp-deployment.yaml

Forcefully deleting a deployment

If a deployment is stuck in the Terminating state, you can forcefully delete it using the --force flag. Use the following command to forcefully delete a deployment:

kubectl delete deployment <deployment-name> --force

Replace <deployment-name> with the name of the deployment you want to forcefully delete.

How to confirm the deletion of a deployment

To confirm that a deployment has been deleted, use the following command:

kubectl get deployments

This command will list all the deployments in the current namespace. If the deployment you just deleted is not listed

How to confirm the deletion of a deployment

To confirm that a deployment has been deleted, use the following command:

kubectl get deployments

This command will list all the deployments in the current namespace. If the deployment you just deleted is not listed, it means that it has been successfully deleted.

Recovering a deleted deployment

If you accidentally delete a deployment, you can recover it using the kubectl rollout command. Use the following command to recover a deleted deployment:

kubectl rollout undo deployment/<deployment-name>

Replace <deployment-name> with the name of the deployment you want to recover. This command will roll back the deployment to the previous version.

Conclusion

Kubectl is a powerful command-line tool used for managing Kubernetes clusters. Deleting a deployment is a common task that you may need to perform. In this article, we discussed how to use the kubectl delete deployment command and provided examples to help you understand the process. You can delete a deployment by name, label selector, or using a manifest file. You can also forcefully delete a deployment and recover a deleted deployment using the kubectl rollout command.