Kubernetes

How to Use Kubectl Delete Deployment in Kubernetes: Examples & Tricks

kubectl delete deployment

Spacelift and Kubernetes

Manage the challenges of Kubernetes with a GitOps flow, policies, and the ability to communicate between stacks from your choice of IaC tools.

Book a demo

In this short article, we will discuss how to use the kubectl delete deployment command in Kubernetes (K8S), explaining how to use it in a step-by-step tutorial with examples.

We will cover:

  1. How to use kubectl delete deployment command
  2. Kubectl delete deployment examples

How to use kubectl delete deployment command

The kubectl delete deployment command can be used to delete Deployments in K8S. It should be used with caution, and always double-check that the YAML files you are issuing the command against contain what you expect before going ahead.

How to delete a Deployment from kubectl?

  1. Open a terminal or command prompt and connect to your K8S cluster.
  2. View a list of deployments with the kubectl get deployment command.
    Use the -n <namespace> flag to specify the namespace of the deployment.
  3. Delete the deployment with the kubectl delete deployment <deployment name> -n <namespace name>.
    For example, if you had a deployment named blog-deployment in the blog namespace, you would run kubectl delete deployment blog-deployment -n blog .

One of the useful options for the kubectl delete deployment command is:

--grace-period=-1 – Period of time in seconds given to the resource to terminate gracefully. Ignored if negative. Set to 1 for immediate shutdown. It can only be set to 0 when — force is true (force deletion).

What happens when you delete a Deployment in Kubernetes?

When you delete a deployment object, K8S first marks the Deployment for deletion in its control plane. The control plane ensures that the desired state of the deployment is removed from the system.

Next, the Deployment controller in K8S that is responsible for managing the desired number of replicas (pods) specified in the deployment configuration starts scaling down the number of pods to zero. This involves terminating the existing pods gracefully by sending a SIGTERM signal to the pod’s main process, allowing it to perform any necessary cleanup or shutdown activities. The grace period for termination is defined in the deployment’s pod termination settings.

Once the termination grace period is reached, K8S sends a SIGKILL signal to forcefully terminate the pod if it hasn’t terminated on its own. The pod is then removed from the node.

As pods are terminated and removed, the actual state of the Deployment aligns with the desired state of having zero replicas.

Once all the pods have been terminated and removed, K8S considers the Deployment successfully deleted. The Deployment object is removed from the K8S control plane.

Note that while the deployment object is deleted, the underlying containers and their images are not deleted automatically.

Also, if the deployment managed any associated resources like ConfigMaps, Kubernetes Secrets, or PersistentVolumes, those resources might still exist unless they were specifically deleted.

Kubernetes operates asynchronously, and the actual timing of events might vary based on cluster load, configuration settings, and other factors.

Check out also how to use kubectl delete pod command.

Kubectl delete deployment examples

Example 1 – How to delete all deployments inside the default namespace

To delete all deployments inside the default namespace, use:

kubectl delete deployment --all --namespace=default

--all: This option indicates that you want to delete all resources of the specified type.

Be cautious when using commands that delete multiple resources!

Example 2 — How to delete Kubernetes deployment from a specific namespace

You can delete a K8S deployment from a specific namespace with:

kubectl delete deployment <deployment name> --namespace <namespace name>

For example, if you had a deployment named blog-deployment in the blog namespace, you would run kubectl delete deployment blog-deployment -n blog.

Example 3 – How to delete all deployments in all namespaces

To delete all deployments in all namespaces, use:

kubectl delete deployment --all --all-namespaces=true

You can also use the -A option, which is an alias to --all-namespaces.

Read more about Kubernetes namespaces.

Example 4 – How to delete multiple deployments

To delete multiple deployments in K8S, you can use a loop in your shell to iterate through a list of deployment names and delete each deployment one by one.

The example file below uses Bash as the shell, which will delete three deployments for the blog namespace:

delete_deployments.sh

#!/bin/bash

# List of deployment names to delete
declare -a deployment_names=("blog-deployment-1" "blog-deployment-2" "blog-deployment-3")

# Namespace where the deployments are located
namespace="blog"

# Loop through the deployment names and delete each deployment
for deployment_name in "${deployment_names[@]}"; do
    kubectl delete deployment "$deployment_name" --namespace="$namespace"
done

To execute the script, first make it executable:

chmod +x delete_deployments.sh

Then run the script:

./delete_deployments.sh

Example 5 — How to delete Kubernetes deployments using its YAML configuration file

If you had a file with a deployment defined in it named blog-deployment.yaml  you could run:

kubectl delete -f blog-deployment.yaml

The -f flag (alias to --filename) is followed by the path containing the resource to delete.

You can also use this approach to delete multiple deployments by specifying multiple file paths:

kubectl delete -f blog-deployment-1.yaml -f blog-deployment-2.yaml

Key points

In this post we learned how to use the kubectl delete deployment command for deleting Kubernetes deployments with examples for specific namespaces, multiple deployments, and how to delete a Kubernetes deployment using the YAML configuration file.

Also, anything that can be run via kubectl can be run within a Spacelift stack. Spacelift helps you manage the complexities and compliance challenges of using Kubernetes. It brings with it a GitOps flow, so your Kubernetes Deployments are synced with your Kubernetes Stacks, and pull requests show you a preview of what they’re planning to change. It also has an extensive selection of policies, which lets you automate compliance checks and build complex multi-stack workflows.

The most Flexible CI/CD Automation Tool

Spacelift is an alternative to using homegrown solutions on top of a generic CI. It helps overcome common state management issues and adds several must-have capabilities s for infrastructure management.

Start free trial