In this guide, we will talk about kubectl delete deployment right from different ways of creating a deployment to 6 different ways to delete deployments in Kubernetes. 

kubectl delete deployment, kubernetes delete deployment, kubectl delete deployment banner

Today, we're going to dive into one of its essential commands: `kubectl delete deployment`. 

Don't worry, we'll keep it straightforward and cut through the jargon. Buckle up as we explore creating and deleting deployments, both single and multiple. 

Prerequisites

  1. minikube

  2. Docker Engine / Desktop(or other 3rd party container service)

  3. kubectl CLI

Do a quick Google search on how to install each of these or follow the provided links and you are good to go then.

Also Read: Kubectl Cheat Sheet

Create a Deployment

In short, a deployment in Kubernetes helps manage your containerized applications, ensuring they run reliably and efficiently. 

Let's start by creating a simple deployment.

Assuming you've got your YAML file ready, use this command to create a deployment:

kubectl create -f deployment.yaml

Replace `deployment.yaml` with your actual YAML file. This will launch your application into the cluster and start managing it.

OR

You can also create different Deployments that run three different web servers.

Open a terminal instance and run the following commands to create 3 Deployments: "one-nginx", "two-apache", and "three-tomcat".

kubectl create deployment one-nginx --image=nginx
kubectl create deployment two-apache --image=httpd
kubectl create deployment three-tomcat --image=tomcat

Note: Run minikube start with your docker instance running in the background to install first-time necessary installations and then proceed with creating deployments. This is what its output will look like.

kubectl create deployment output

Also Read: The Only Guide on Kubernetes Namespaces You'll Ever Need

Create a Deployment Inside a Namespace

Namespace segregation is crucial for managing large projects. If you want to create a deployment within a specific namespace, follow this command:

kubectl create -f deployment.yaml -n your-namespace

OR 

Let’s make some Namespaces without using YAML files. 

In order to create Deployments inside specific Namespaces, we must create some Namespaces first.  

For the purposes of this example, we will create two Namespaces: "dev" and "prod". 

kubectl create namespace dev
kubectl create namespace pro

This is what its output look like:

kubectl create namespace output

We can now create Deployments inside each of them. 

Create two Deployments inside the "dev" Namespace and one inside the "prod" Namespace. 

kubectl create deployment dev-1 --image=busybox -n=dev
kubectl create deployment dev-2 --image=alpine -n=dev
kubectl create deployment prod-1 --image=archlinux -n=prod

Here, -n stands for namespace. 

Let's look at the output.

kubectl create deployment output

Note:  "busybox", "alpine", and "archlinux" are Linux distributions.

To summarize, we have created a total of 6 deployments in 3 different Namespaces. We have 3 Deployments inside the "default" namespace:  

To view them, run kubectl get deployments to get all the deployments in the default namespace and kubectl get deployments -n=dev to get deployments in the dev namespace, and kubectl get deployments -n=prod to get deployments in the prod namespace. 

The output will look something like this.

kubectl get deployments output

For dev:

kubectl get deployments dev output

For prod:

kubectl get deployments prod output
Humalect developer platform CTA banner, kubectl delete deployment

Delete a Deployment

Let’s take a look at the general syntax of how to delete a Kubernetes resource first then we’ll delve into deleting a deployment:

kubectl delete ([-f FILENAME] | TYPE [(NAME | --all)])

  • `[-f FILENAME]`: This is an optional flag that allows you to specify a YAML or JSON file containing Kubernetes resource definitions. The `kubectl` tool will read the file and delete the resources defined in it.
  • `TYPE`: The type of Kubernetes resource you want to delete. For example, this could be `pod`, `deployment`, `service`, `configmap`, etc.
  • `NAME | --all`: This part specifies the name of the resource you want to delete. You can provide the specific name of a single resource to delete that resource. Alternatively, you can use the `--all` flag to delete all resources of the specified type. If you use `--all`, the `NAME` argument is not required.

To find out all deployments across all namespaces, use this command: 

kubectl get deploy -A

Let's look at the output.

Time to face the music and delete a deployment. It's as simple as this command:

kubectl delete deployment your-deployment-name

Replace `your-deployment-name` with the actual name of your deployment. Confirm the deletion with a 'y' when prompted.

For example, 

kubectl delete deployment one-ngnix

This command will delete one-ngnix deployment from the default namespace.

Also Read: How to Use Just One LoadBalancer for Multiple Apps in Kubernetes?

Deleting Multiple Deployments

The cleanup party continues. To remove multiple deployments at once, you can separate their names with spaces in the `kubectl delete' command:

kubectl delete deployment deployment1 deployment2 deployment

Replace `deployment1`, `deployment2`, and `deployment3` with the names of the deployments you want to obliterate. 

Confirm each deletion with 'y' when prompted.

Here's an example: 

kubectl delete deployment one-nginx two-apache

The above will nuke one-ngnix and two-apache deployments from the default namespace we spawned. 

Let's look at its output.

kubectl delete deployment output

Now we are left with one deployment in default. 

kubectl get deployments output

Also Read: Kubectl Config Set Context Tutorial

Delete All Deployments Inside the Default Namespace

If you're itching to clear out your default namespace, here's how you can wipe out all deployments residing there:

kubectl delete deployment --all

Execute this command, and Kubernetes will nuke all deployments within the default namespace. Confirm with 'y' when prompted.

kubectl delete deployment --all output
Humalect developer platform cta, kubectl delete deployments

Delete Deployment(s) in a Specific Namespace

Deleting deployments within a specific namespace is a breeze. Just tailor your command like this:

kubectl delete deployment your-deployment-name -n your-namespace

Replace `your-deployment-name` with the actual name of the deployment and `your-namespace` with the desired namespace.

Here's an example:

kubectl delete deployment dev-1 -n=dev

Output:

kubectl delete deployment in a specific namespace output

Also Read: Docker Commands Cheat Sheet

Delete All Deployments in All Namespaces

To erase all deployments across all namespaces:

kubectl delete deployment --all --all-namespaces

This command doesn't mess around—it wipes the slate clean everywhere. 

kubectl delete deployment --all output

Also Read: How to Cleanup Docker Resources?

Delete a Deployment Using its YAML Configuration File

In the previous sections, we learned to delete Deployments using their names, but what if you've got the YAML file that birthed the deployment? 

Well, we have a way to delete it using its own blueprint:

kubectl delete -f deployment.yaml

Replace `deployment.yaml` with your actual YAML file. This command reads the file and identifies the resources to remove.

Example? Isn’t it self-explanatory? 

Example incoming!

Step 1: Create a Deployment YAML File

Create a file named `deployment.yaml` with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: nginx:latest

This YAML defines a Kubernetes Deployment with 3 replicas running the Nginx web server.

Also Read: How to Keep Docker Running?

Step 2: Create the Deployment

Run the following command to create the deployment using the YAML file:

kubectl apply -f deployment.yaml

This will create the Deployment and its associated resources.

Step 3: Verify Deployment

To check if the deployment was created successfully, you can use the following command:

kubectl get deployment my-deployment

Step 4: Delete the Deployment

To nuke this deployment, use the following command:

kubectl delete deployment my-deployment

This will remove the Deployment and its associated resources.

Keep in mind that these instructions assume you have a Kubernetes cluster set up and `kubectl` configured to interact with the cluster. 

A word of wisdom: Always double-check and test configurations in a non-production environment before deploying them to production or else brace for a big impact! 

Also Read: How to Manage Kubernetes Cluster using Kubeadm?

What Happens When You Delete a Deployment in Kubernetes?

When you delete a deployment, Kubernetes gracefully winds down the replica sets and pods associated with it. The desired number of pods decreases over time, leaving your cluster in a balanced state. 

If you ever need to revert, don't worry! Kubernetes remembers your deployment's configuration, making it easy to bring everything back to life.

Also Read: Kubernetes Events Guide

Delete Using Deployment Descriptor

Another way to delete an existing deployment is by using the deployment descriptor file. 

kubectl --context my-context -n my-namespace delete -f deployment-descriptor.yaml

Output:

deployment.apps "my-deployment" deleted

If by chance you happen to delete the wrong deployment, you can then recreate it by:-

kubectl apply -f deployment-descriptor.yaml

Output:

deployment.apps/my-deployment created
kubectl delete deployment, humalect developer platform cta banner

Final Words: Kubectl Delete Deployment

We've journeyed from creating deployments, including those within namespaces, to wielding the power of deletion for both single and multiple deployments. 

Keep your Kubernetes cluster tidy and organized with these simple commands.

Remember, simplicity is key. No need to drown in corporate-speak or obscure terminology. You can manage your deployments like a Kubernetes pro with just a few commands. 

Kubernetes is EASY! 

So go forth, command your deployments, and conquer the world of containerized applications!

And remember, when you delete a deployment, Kubernetes ensures a graceful exit for your pods. Until next time, keep the commands sharp and your deployments sharper!

Happy nuking-the-pods!