Skip to main content
Kubernetes

How to Delete a Kubernetes Deployment [Quick K8s Tips]

Here are two ways to delete Kubernetes deployments.

Rakesh Jain

A Kubernetes Deployment runs multiple replicas of your application and automatically replaces any instances that fail or become unresponsive.

When you are practicing Kubernetes, you'll often need to delete Kubernetes deployments.

Deleting deployments is easy, thanks to the kubectl delete deployments command:

kubectl delete deployment deployment_name

I'll discuss it in a bit more detail with steps to get the deployment name. I'll also show you another way of deleting deployments in Kubernetes.

Use kubectl delete deployment command for deleting Kubernetes deployments

Though it usually gets tab completed, you would be better with the name of the Deployment you want to delete.

root@kmaster-rj:~# kubectl get deployments
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
my-dep   2/2     2            2           4m22s

Once you have the Deployment name, simply use it like this:

kubectl delete deployments my-dep

It should show an output telling you that your deployment is deleted:

root@kmaster-rj:~/pod-create# kubectl delete deployments my-dep
deployment.apps "my-dep" deleted

You may verify it by checking the available deployments:

root@kmaster-rj:~/pod-create# kubectl get deployments
No resources found in default namespace.
You may also use deploy or deployments (with an s) instead of deployment in the kubectl delete deployment command. It's all the same.

Deleting Kubernetes Deployment from specific namespace

The kubectl delete deployments command deletes deployments which are present in "default" namespace unless you provide a different namespace.

What is namespace in Kubernetes?

Namespaces are intended for use in environments with many users spread across multiple teams, or projects. It is like a separate a virtual cluster within Kubernetes cluster.

For example, you can have separate namespaces to isolate your workloads for production, staging and development environments.

List all available namespaces and their deployments

To see all the namespaces, use the following command:

root@kmaster-rj:~# kubectl get namespaces
NAME              STATUS   AGE
default           Active   46d
kube-node-lease   Active   46d
kube-public       Active   46d
kube-system       Active   46d
webapps           Active   22d

You may also list all the deployments from all the namespaces:

root@kmaster-rj:~# kubectl get deployments --all-namespaces
NAMESPACE     NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
default       my-dep2                   2/2     2            2           3h29m
kube-system   calico-kube-controllers   1/1     1            1           46d
kube-system   coredns                   2/2     2            2           46d
webapps       webapps-dep               2/2     2            2           5h19m

When you have the namespace and the deployment name, you can specify it to the kubectl delete deployment command:

root@kmaster-rj:~# kubectl delete deployments --namespace=webapps webapps-dep
deployment.apps "webapps-dep" deleted

As you can see, webapps-dep has been deleted.

root@kmaster-rj:~# kubectl get deployments --all-namespaces
NAMESPACE     NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
default       my-dep2                   2/2     2            2           3h29m
kube-system   calico-kube-controllers   1/1     1            1           46d
kube-system   coredns                   2/2     2            2           46d

Deleting multiple deployments

You can delete more than one Kubernetes deployments by providing their names like this:

root@kmaster-rj:~/pod-create# kubectl delete deployment my-dep my-dep-2 --namespace=default
Linux Foundation Kubernetes Certification Discount

Deleting Kubernetes deployments using its YAML configuration file

You may also use the YAML configuration file to delete the resource associated with it.

root@kmaster-rj:~/pod-create# kubectl delete -f deployment-definition.yml
deployment.apps "my-dep" deleted

Let me show it to you step by step.

Create a new deployment using deployment-definition.yml file:

root@kmaster-rj:~# cat pod-create/deployment-definition.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-dep
  labels:
    app: webapp
  namespace: default

spec:
  template:
    metadata:
      name: my-dep-pods
      labels:
        app: webapp
        tier: front-end
    spec:
      containers:
        - name: httpd
          image: httpd
          imagePullPolicy: IfNotPresent
          ports:
          - containerPort: 80
  replicas: 2
  selector:
    matchLabels:
      tier: front-end

Create the deployment using the above YAML file:

root@kmaster-rj:~/pod-create# kubectl apply -f deployment-definition.yml
deployment.apps/my-dep created

You can see the Deployment now:

root@kmaster-rj:~# kubectl get deployments --all-namespaces
NAMESPACE     NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
default       my-dep                    2/2     2            2           1m13s

Now, if you refer the same YAML file for deletion purpose, it will delete the resources (deployments here) associated with that file.

root@kmaster-rj:~/pod-create# kubectl delete -f deployment-definition.yml
deployment.apps "my-dep" deleted

You may verify it by listing all the deployments:

root@kmaster-rj:~/pod-create# kubectl get deployments
No resources found in default namespace.

Hope you like this quick K8s tip. We'll be covering more Kubernetes tutorials. Do subscribe for more learning materials.

Rakesh Jain
India