How to Completely Remove a Kubernetes Deployment (with Examples)

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

When it comes to managing containerized applications, Kubernetes is a powerful orchestrator that ensures your deployments are running as intended. However, there will be times during the lifecycle of an application when you need to remove a deployment from your cluster. In this tutorial, we’ll explore how to completely remove a Kubernetes deployment, including cleaning up any related resources to leave your system tidy and free of any leftovers.

Before we begin, ensure that you have kubectl installed and correctly configured to interact with your cluster.

Basic Removal of a Deployment

$ kubectl delete deployment [deployment-name]

This command will delete the specified deployment, freeing up resources. However, this alone might not remove all the resources created with the deployment such as services, persistent volume claims, or configmaps.

Cleaning Up Related Resources

Deployments in Kubernetes can create a range of objects. Running the following command will show you all the related resources that are created when a deployment is applied.

$ kubectl get all --selector app=[app-label]

This should list all the related resources like services, pods, replica sets, etc. To remove these resources, you need to delete each one individually.

Services

$ kubectl delete svc [service-name]

ReplicaSets

$ kubectl delete rs [replicaset-name]

Pods

Pods are usually deleted along with the deployment, but you can delete any strays with:

$ kubectl delete pod [pod-name]

Using Labels for Deletion

Applying labels to your resources is a best practice in Kubernetes, as it allows you to manage and select resources as a group.

$ kubectl delete all --selector app=[app-label]

This will delete all resources with the given label across pods, replica sets, and services.

Deleting Persistent Volumes

If your deployment used any persistent volumes, they would need to be deleted separately. First, you should delete the persistent volume claims (PVCs) that were created with your deployment.

$ kubectl delete pvc [pvc-name]

Afterward, depending on your PVC configuration, you might need to manually delete the associated persistent volumes (PVs) as well.

$ kubectl delete pv [pv-name]

Cleaning Up ConfigMaps and Secrets

Configuration data and secrets that were part of the deployment should also be removed.

ConfigMaps

$ kubectl delete configmap [configmap-name]

Secrets

$ kubectl delete secret [secret-name]

Advanced Cleanup Using Namespaces

For more advanced management, you might have deployed your applications within a namespace. To remove all resources within a given namespace, including the namespace itself:

$ kubectl delete namespace [namespace-name]

This results in the deletion of all associated resources within the namespace.

Conclusion

In this tutorial, we’ve covered the essential commands to completely remove a Kubernetes deployment and all related resources. As a final reminder, be extra cautious when deleting resources, especially in production environments, to ensure that you’re not disrupting any applications that should remain active.