kubernetes deployment

How to Destroy a Kubernetes Deployment

In this article we will learn how to destroy a Kubernetes deployment easily from the command line. This is useful when you are playing around with Kubernetes or when you are doing maintenance work and you need to remove unnecessary deployments after a migration or something similar.

Let’s start!

Delete Kubernetes Deployment By Name

One way to destroy an existing deployment is by using kubectl delete deployment command. To be able to use this command, we’ll need the deployment name.

In order to get the deployment name, you will need to know what context and namespace is your deployment located in. Once we have them, we can list the deployment by running the following:

$ kubectl --context my-context -n my-namespace get deployments

NAME              READY   UP-TO-DATE   AVAILABLE   AGE
my-deployment     1/1     1            1           1y123d

In this case, you can see that the name of our deployment is my-deployment, let’s try deleting it then.

$ kubectl --context my-context -n my-namespace delete deployment my-deployment
deployment.apps "my-deployment" deleted

If everything went well, you should see a message confirming that the deployment has been deleted.

You can confirm that the deployment no longer exists by getting the existing deployments again:

$ kubectl --context my-context -n my-namespace get deployments
No resources found in default namespace.

That’s it! As simple as this.

There’s also another way to destroy a deployment.

Delete Using Deployment Descriptor

Another way to destroy an existing deployment is by using the deployment descriptor file. For instace:

$ kubectl --context my-context -n my-namespace delete -f deployment-descriptor.yaml
deployment.apps "my-deployment" deleted

If for any reason you deleted the wrong deployment by accident, you can recreate it by doing the following:

$ kubectl apply -f deployment-descriptor.yaml
deployment.apps/my-deployment created

Deleting Multiple Kubernetes Deployments

One more thing worth mentioning is that you have the ability to delete multiple namespaces in one go. You just have to pass a list of deployment names, so you could then run the following:

$ kubectl --context my-context -n my-namespace delete deployment my-deployment1 my-deployment2

Simple, right? You must agree that Kubernetes is wonderful!

If you are interested in learning more about Kubernetes, we highly recommend the following books:

Conclusion

In this short article we’ve seen how easy is to destroy a deployment in Kubernetes. Please also be aware that you should not do this in production deployments, unless that you are absolutely certain that the deployment is currently not receiving any traffic at all.

That’s all from us today! We hope you enjoyed this article!

Thanks for reading us and see you soon again!