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:
$ kubectl delete deployment <deployment>
Run in Warp
Where:
- deployment is the name of the deployment you want to delete.
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:
$ kubectl delete deployment --all
Run in Warp
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 ):
$ kubectl get deployment -A
Run in Warp
Then use the kubectl delete command with the -n flag (short for --namespace ) as follows:
$ kubectl delete deployment <deployment> -n <namespace>
Run in Warp
Where:
- deployment is the name of the deployment you want to delete.
- namespace is the name of the sub-cluster that contains this deployment.
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:
$ kubectl delete deployment <deployment> -n <namespace> --force=true
Run in Warp
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:
$ kubectl delete deployment <deployment>--grace-period=<period>
Run in Warp
Where:
- period is the number of seconds given to a Deployment before being terminated.
For example:
$ kubectl delete deployment my-deployment --grace-period=30
Run in Warp
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:
$ kubectl delete -f <file>
Run in Warp
Where:
- file is the path to the YAML or JSON file containing the Deployment's configuration.
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:
$ kubectl delete -k <directory>
Run in Warp
Where:
- directory is the name of the directory that contains the Kustomization file.
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:
$ kubectl scale --replicas=0 deployment/<deployment> -n <namespace>
Run in Warp
Where:
- replicas is used to set the number of Pod replicas to 0.
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:
$ kubectl delete pod --all -n <namespace>
Run in Warp
Finally, you can delete the Deployment itself using the following command:
$ kubectl delete deployment <deployment> -n <namespace>
Run in Warp
You can learn more about deleting Pods with our article on how to delete Kubernetes Pods using the kubectl command.
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:
$ kubectl get service -n <namespace>
Run in Warp
Then, you can delete all the Services using the --all flag as follows:
$ kubectl delete service --all -n <namespace>
Run in Warp
Finally, you can delete the Deployment itself using the following command:
$ kubectl delete deployment <deployment> -n <namespace>
Run in Warp
Written by
Ekene Ejike
Filed Under
Related Articles
Copy Files From Pod in Kubernetes
Learn how to copy files and directories from within a Kubernetes Pod into the local filesystem using the kubectl command.
Scale Deployments in Kubernetes
Learn how to manually and automatically scale a Deployment based on CPU usage in Kubernetes using the kubectl-scale and kubectl-autoscale commands.
Get Kubernetes Logs With kubectl
Learn how to get the logs of pods, containers, deployments, and services in Kubernetes using the kubectl command. Troubleshoot a cluster stuck in CrashloopBackoff, ImagePullBackoff, or Pending error states.
Forward Ports In Kubernetes
Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward command.
Tail Logs In Kubernetes
Learn how to tail and monitor Kubernetes logs efficiently to debug, trace, and troubleshoot errors more easily using the kubectl command.
Get Context In Kubernetes
Learn how to get information about one or more contexts in Kubernetes using the kubectl command.
Delete Kubernetes Namespaces With kubectl
Learn how to delete one or more namespaces and their related resources in a Kubernetes cluster using the kubectl command.
Get Kubernetes Secrets With kubectl
Learn how to list, describe, customize, sort and filter secrets in a Kubernetes cluster by name, type, namespace, label and more using the kubectl command.
List Kubernetes Namespaces With kubectl
Learn how to list, describe, customize, sort and filter namespaces in a Kubernetes cluster by name, label, and more using the kubectl command.
How To List Events With kubectl
Learn how to list and filter events in Kubernetes cluster by namespace, pod name and more using the kubectl command.
Kubernetes vs Docker: The Backbone of Modern Backend Technologies
Lean the fundamentals of the Kubernetes and Docker technologies and how they interplay with each other.
Set Context With kubectl
Learn how to create, modify, switch, and delete a context in Kubernetes using the kubectl config command.