How To Delete A Kubernetes Deployment With kubectl | Warp

Introducing Warp Factories

open, flexible infrastructure for building your own cloud software factory. Learn More

In kubernetes, Deployments are used to manage, create or modify an instance of a Pod that runs a containerized application. There are several reasons why you need to delete a deployment and you can easily interact with these deployments using the kubectl get command.

The short answer

In kubernetes, to delete a Deployment, you can use the kubectl delete command as follows:

Bash

$ kubectl delete deployment <deployment>

Where:

Easily retrieve this command using Warp’s AI Command Suggestions

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering k8s delete deployment in the AI Command Suggestions will prompt an kubectl command that can then quickly be inserted into your shell by doing CMD+ENTER.

Deleting all Deployments

To delete all the Deployments in a Kubernetes cluster at once, you can use the kubectl delete command with the --all flag as follows:

Bash

$ kubectl delete deployment --all

Deleting a Deployment in a namespace

To delete a Deployment in a namespace, you can start by first listing all the Deployments in your Kubernetes cluster using the kubectl get command with the -A flag (short for --all-namespaces):

Bash

$ kubectl get deployment -A

Then use the kubectl delete command with the -n flag (short for --namespace) as follows:

Bash

$ kubectl delete deployment <deployment> -n <namespace>

Where:

Forcing the deletion of a Deployment

Oftentimes, deleting a Deployment forcefully can be useful if you want to start the Deployment afresh or if something went wrong during the initial deployment.

To forcefully delete a Deployment, you can use the --force=true flag as follows:

Bash

$ kubectl delete deployment <deployment> -n <namespace> --force=true

Deleting a Deployment with a grace period

In Kubernetes, the grace period refers to the amount of time given to a Deployment or a Pod to perform any necessary cleanup or shutdown tasks before it is forcefully terminated.

To delete a Deployment with a grace period, you can use the --grace-period flag as follows:

Bash

$ kubectl delete deployment <deployment> --grace-period=<period>

Where:

For example:

Bash

$ kubectl delete deployment my-deployment --grace-period=30

This command will delete the deployment named my-deployment with a grace period of 30 seconds.

Note that when period is set to 0, Kubernetes will immediately terminate the Pods without waiting for them to gracefully terminate. And when set to -1, Kubernetes will use the default grace period defined in the terminationGracePeriodSeconds property in the Pod specification.

Deleting a Deployment using a YAML or JSON file

To delete a Deployment based on the YAML or JSON manifest it was created from, you can use the -f flag (short for --filename) as follows:

Bash

$ kubectl delete -f <file>

Where:

Note that before running this command, you should carefully review the content of the file to avoid unintended deletions of the other resources it may contain.

Deployment using a Kustomization file

In Kubernetes, a Kustomization file is a YAML file that allows you to define and manage multiple resources without modifying the original configuration file.

To delete a Deployment created from a Kustomization file, you can use the -k flag (short for --kustomize) as follows:

Bash

$ kubectl delete -k <directory>

Where:

Deleting a Deployment and its associated Pods

To delete a Deployment and all the Pods associated with it, you can first scale down the Deployment to prevent the Pods from spawning up again after deletion using the kubectl scale command:

Bash

$ kubectl scale --replicas=0 deployment/<deployment> -n <namespace>

Where:

After the Deployment has been scaled down to zero, you can delete all the Pods in a specified namespace using the --all flag as follows:

Bash

$ kubectl delete pod --all -n <namespace>

Finally, you can delete the Deployment itself using the following command:

Bash

$ kubectl delete deployment <deployment> -n <namespace>

Deleting a Deployment and its associated Services

To delete a Deployment and the Services associated with it, you can first list out the services specific to the Deployment using the kubectl get command with the --namespace flag as follows:

Bash

$ kubectl get service -n <namespace>

Then, you can delete all the Services using the --all flag as follows:

Bash

$ kubectl delete service --all -n <namespace>

Finally, you can delete the Deployment itself using the following command:

Bash

$ kubectl delete deployment <deployment> -n <namespace>