Terminus
Delete Kubernetes Deployments With kubectl

Delete Kubernetes Deployments With kubectl

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 [.inline-code] kubectl get[.inline-code]  command.

The short answer

In kubernetes, to delete a Deployment, you can use the [.inline-code] kubectl delete[.inline-code]  command as follows:

$ kubectl delete deployment <deployment>

Where:

  • [.inline-code] deployment[.inline-code]  is the name of the deployment you want to delete.

[#easily-recall-syntax-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

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

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

[#delete-all-deployments] Deleting all Deployments [#delete-all-deployments]

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

$ kubectl delete deployment --all

[#delete-deployment-in-a-namespace] Deleting a Deployment in a namespace [#delete-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 [.inline-code] kubectl get[.inline-code]  command with the [.inline-code] -A[.inline-code]  flag (short for [.inline-code] --all-namespaces[.inline-code] ):

$ kubectl get deployment -A

Then use the [.inline-code] kubectl delete[.inline-code]  command with the [.inline-code] -n[.inline-code]  flag (short for [.inline-code] --namespace[.inline-code] ) as follows:

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

Where:

  • [.inline-code] deployment[.inline-code]  is the name of the deployment you want to delete.
  • [.inline-code] namespace[.inline-code]  is the name of the sub-cluster that contains this deployment.

[#delete-deployment-forcefully] Forcing the deletion of a Deployment [#delete-deployment-forcefully]

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 [.inline-code] --force=true[.inline-code]  flag as follows:

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

[#delete-deployment-with-a-grace-period] Deleting a Deployment with a grace period [#delete-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 [.inline-code] --grace-period[.inline-code]  flag as follows:

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

Where:

  • [.inline-code] period[.inline-code]  is the number of seconds given to a Deployment before being terminated.

For example:

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

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

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

[#delete-deployment-with-yaml-or-json] Deleting a Deployment using a YAML or JSON file [#delete-deployment-with-yaml-or-json]

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

$ kubectl delete -f <file>

Where:

  • [.inline-code] file[.inline-code]  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.

[#delete-deployment-with-kustomization] Deployment using a Kustomization file [#delete-deployment-with-kustomization]

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 [.inline-code] -k[.inline-code]  flag (short for [.inline-code] --kustomize[.inline-code] ) as follows:

$ kubectl delete -k <directory>

Where:

  • [.inline-code] directory[.inline-code]  is the name of the directory that contains the Kustomization file.

[#delete-deployment-with-pods] Deleting a Deployment and its associated Pods [#delete-deployment-with-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 [.inline-code] kubectl scale[.inline-code] command:

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

Where:

  • [.inline-code] replicas[.inline-code]  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 [.inline-code] --all[.inline-code]  flag as follows:

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

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

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

You can learn more about deleting Pods with our article on how to delete Kubernetes Pods using the kubectl command.

[#delete-deployment-with-services] Deleting a Deployment and its associated Services [#delete-deployment-with-services]

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

$ kubectl get service -n <namespace>

Then, you can delete all the Services using the [.inline-code] --all[.inline-code]  flag as follows:

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

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

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