How To Remove a Deployment in Kubernetes: Methods, Steps and Tools
Tired of orphaned pods and volumes? Learn how to remove a Deployment in Kubernetes in five easy steps and keep your cluster lean and ready for the next Deployment.
May 22nd, 2025 5:00am by
Photo by Mick Haupt on Unsplash.
- To delete a Deployment and its associated resources, use:
kubectl delete deployment <deployment-name> -n <namespace> - Deleting a Deployment doesn’t remove associated PVCs or PVs. You must explicitly delete these resources to prevent orphaned volumes:
kubectl delete pvc <pvc-name> -n <namespace> - Use
kubectl apply --pruneto remove resources no longer defined in your manifests. Set ttlSecondsAfterFinished for Jobs or configure CronJob history limits to clean up completed workloads automatically.
Should You Delete or Scale Down?
Scaling down a Deployment in Kubernetes by setting its replica count to zero stops all associated pods, releases compute resources and preserves the deployment configuration for rapid reinstatement later. Scaling down avoids reapplying manifest files and recreating ReplicaSets, enabling swift reactivation of pods without modifying YAML definitions. Deleting a Deployment viakubectl delete deployment fully removes the deployment object, its ReplicaSets, pods and associated metadata from the cluster API, requiring reapplication of manifests for any future redeployment. Deletion also purges event history, logs and status, preventing accidental reuse of misconfigured resources.
You need to choose the right action based on your needs:
Prerequisites and Tools: Removing a Deployment in K8s
Before you delete or scale down a Deployment, make sure you have the right tools in place and the necessary access to your Kubernetes cluster. It will ensure that you can carry out operations safely, avoid misconfigurations, and roll back easily if something goes wrong. Here’s what you’ll need:- kubectl command line interface (CLI) installed: The primary command-line tool for interacting with your cluster.
- Configured kubeconfig: Your local kubeconfig file should point to the correct cluster context and namespace.
- Cluster-level permissions: Role-based access control (RBAC) or cluster-role bindings that allow you to get, list, scale, and delete Deployments.
- YAML manifests or Helm charts: Definitions of your Deployment objects for reference or redeployment.
- Backup or version control: A record of your manifests (such as git) or a snapshot tool in case you need to roll back.
- Monitoring and logging tools (optional but recommended): Systems like Prometheus, Grafana, or EFK/ELK to observe the impact of your changes.
4 Methods for Removing a Deployment in Kubernetes
Below are four common approaches for tearing down deployments in Kubernetes. Choose the one that best fits your CI/CD process and operational requirements.1. kubectl delete deployment Command
Use the built-in CLI for a one-off deletion:
kubectl delete deployment <deployment-name> [-n <namespace>]
kubectl delete: This is the command used for deleting resources.deployment: It specifies the kind of resource you want to delete.<deployment-name>: This is where you put the actual name of the Deployment you want to remove.[-n <namespace>]: This is an optional flag to specify the namespace where the Deployment is located. If you omit this, kubectl will assume the current or default namespace.
kubectl delete deployment my-app
And if it’s in a namespace called staging, you would run: kubectl delete deployment my-app -n staging
2. Deleting via Manifest (kubectl delete -f)
Tie into your git-backed manifests or local YAML files by pointing kubectl at them:
kubectl delete -f path/to/deployment.yaml
kubectl delete: The command for deleting resources.-f: This flag tells kubectl to read the resource configuration from the file specified.path/to/deployment.yaml: This is the path to your manifest file, which could define a Deployment, or any other Kubernetes resource. kubectl will read this file and delete all the resources defined within it.
-f dir/) and label selectors (-l key=value), giving you fine-grained control over which resources to delete.
3. Removing Helm-Managed Deployments (helm uninstall)
When you deploy applications using Helm, use the Helm CLI to uninstall them. This ensures a cleaner removal of all the resources that were part of the Helm release.
helm uninstall <release-name> [-n <namespace>]
helm uninstall: This is the Helm command to uninstall a release.<release-name>: This is the name you gave to your Helm release when you installed it.[-n <namespace>]: This optional flag specifies the namespace where the Helm release was deployed. If omitted, Helm will assume the default namespace.
helm uninstall my-chart
If it was in a namespace called operations, you would run: helm uninstall my-chart -n operations
4. GitOps Deletion (Pull-Request Workflow)
In GitOps, your Git repo is the source of truth — removal happens via code changes:- Delete the Deployment YAML (or comment it out) in your Git branch.
- Open a pull request against your main branch.
- Merge after review, and let your GitOps controller, such as Argo CD and Flux, reconcile and delete the resource.
How To Remove a Deployment in Kubernetes: 5 Easy Steps
Below is a concise, step-by-step walkthrough for safely removing a Deployment from your Kubernetes cluster. You’ll learn how to pinpoint the Deployment, optionally pause it, delete it, verify that all pods and ReplicaSets vanish, and finally clean up any leftover resources.1. Identify the Target Deployment
First, list your Deployments to confirm the exact name and namespace of the workload you intend to remove. Use this command:
kubectl get deployments --all-namespaces
-n <namespace> flag to narrow the output to a specific namespace.
2. Scale Replicas to Zero (Optional)
If you prefer to pause the application before deleting, scale its replica count down to zero by using this command:
kubectl scale deployment <deployment-name> --replicas=0 -n <namespace>
3. Execute the Delete Command
Once you’re ready for a full teardown, delete the Deployment object and its children in one go:
kubectl delete deployment <deployment-name> -n <namespace>
--grace-period=<seconds> or --force.
4. Verify ReplicaSets and Pods Are Gone
After deletion, confirm that no ReplicaSets or Pods related to the Deployment remain. Here’s the command you can use: A successful deletion returns no matching resources. If you still see entries, re-run the delete command or investigate finalizers. Checking these ensures you don’t leave orphaned workloads consuming resources.5. Clean Up Associated Resources (Services, PVCs, ConfigMaps, Secrets)
Deployments often create linked Services, PersistentVolumeClaims (PVCs), ConfigMaps, or Secrets that may not be automatically deleted. Remove them explicitly: Or, delete by label selector to target groups of resources at once:
kubectl delete all,configmap,secret -l app=<deployment-label> -n <namespace>
What Happens Behind the Scenes?
When you delete or scale down a Deployment, Kubernetes doesn’t simply vanish resources — it orchestrates a series of cleanup and shutdown processes to maintain cluster integrity and prevent resource leaks. Here is how it works.Controller Garbage Collection
Kubernetes uses a garbage collector controller to remove dependent resources when their owning object is deleted, similar to how a library system automatically withdraws borrowed books when a patron’s account is closed. Each resource carries an ownerReference that ties it to its parent, so when you delete a Deployment, its ReplicaSets and Pods become “orphans” that the garbage collector then garbage-collects. You can choose between:- Foreground cascading deletion, where Kubernetes deletes all dependents first and only then the parent
- Background cascading deletion, where the parent disappears immediately and dependents are cleaned up asynchronously
Graceful Termination and Finalizers
When you delete a pod, Kubernetes marks it for termination by setting .metadata.deletionTimestamp. During this termination period:- The kubelet sends a SIGTERM signal to the container’s main process and runs any configured preStop hooks, giving your application a chance to close connections and flush buffers.
- If the container doesn’t terminate within terminationGracePeriodSeconds, Kubernetes escalates to a SIGKILL, forcing the shutdown to avoid indefinite hangs.
Challenges in Removing a Deployment (and Fixes)
Below are some of the most frequent snags you’ll encounter when removing Deployments, along with concrete remedies to get you unstuck without derailing your maintenance window.1. Terminating Pods That Won’t Exit
Sometimes, you issue a delete or scale-down, but pods linger in the Terminating status. Think of it like guests refusing to leave a party after you’ve announced closing time. Common causes and fixes include:- Lingering finalizers: Pods awaiting cleanup hooks won’t complete termination until finalizers are removed or their controllers finish work.
- Stuck volume mounts: If a PVC or CSI driver hangs, Kubernetes can’t unmount and terminate the pod. Restarting the kubelet on the node often frees the mount.
- Failed liveness/readiness probes: Misconfigured health checks can prevent graceful shutdown. Verify your probe URLs and timeouts.
- Force deletion: As a last resort, use
kubectl delete pod <name> --grace-period=0 --forceto send SIGKILL immediately. - Investigate with runbooks: Follow structured steps — like inspect finalizers, check node connectivity or restart kubelet — before you escalate.
2. Stuck Finalizers Blocking Deletion
Finalizers act like “do not demolish until cleanup is done” tags on Kubernetes objects. If a controller fails to remove them, the resource stays in Terminating forever. You’ll typically see this on Namespaces, CRDs, or PVCs. To unblock:- Identify the stuck resource with
kubectl get <resource> <name> -o jsonpath='{.metadata.finalizers}'.
- Edit out finalizers by patching the object:
kubectl patch <resource> <name> -p '{"metadata":{"finalizers":[]}}' --type=merge.
- Avoid manual finalizer deletion except as an emergency workaround — removing finalizers bypasses the intended cleanup logic, which can corrupt external systems.
- Prevent recurrence by ensuring your controllers handle their finalizers properly. For instance, use operators that implement cleanup in their code.
3. Orphaned Persistent Volumes
When you delete a Deployment, its PVCs and underlying PVs may linger, like abandoned storage lockers still accruing rent. Left unchecked, they waste space and rack up costs. To detect and remove them:- List unbound PVs —
kubectl get pv --field-selector=status.phase=Releasedshows PVs no longer claimed by any pod. - Automate cleanup with scripts or tools like Kor that scan for PVs in Released or Failed phases and delete them after approval.
- Use label selectors to target application-specific volumes:
kubectl delete pv -l app=myapp. - Integrate alerts in your monitoring stack — Prometheus, Grafana — to notify you of growing PV counts in non-Bound states.
- Adopt life cycle hooks in your Helm chart or GitOps pipelines to automatically remove PVCs and PVs when an application is torn down.
Automating Cleanup
A clean, automated teardown of Kubernetes resources saves time and prevents drift between your declared state and the actual cluster. By combining cascading deletion, pruning, and built-in TTL mechanisms for Jobs and CronJobs, you can ensure that unused or outdated resources disappear without manual intervention. Here’s how to go about each method.Using --cascade and –-prune Flags
Kubernetes gives you flags to control how resources and their dependents are removed or pruned.
Here’s what --cascade on delete offers:
- Foreground deletion (
--cascade=foreground) blocks removal of the parent object until all its dependents are deleted first. - Background deletion
(--cascade=background) removes the parent immediately and lets the garbage collector clean up dependents asynchronously. - Use
kubectl delete deployment my-deploy --cascade=foregroundto enforce ordered cleanup or--cascade=backgroundfor faster returns from the CLI.
kubectl apply -f ./manifests --prune -l app=myapp. Starting in Kubernetes 1.27, the new ApplySet-based pruning (--applyset) improves safety and performance by explicitly tracking resource sets.
ttlAfterFinished and CronJobs
Kubernetes provides built-in features to automatically clean up completed Jobs, such as.- ttlSecondsAfterFinished for Jobs, which lets you add
.spec.ttlSecondsAfterFinished: <seconds>to a Job manifest to have the TTL-after-finished controller delete it (and its dependents) once the timer expires. This feature is in general availability as of v1.23 and honors finalizers, ensuring safe teardown. - History limits for CronJobs, where you can Use
.spec.successfulJobsHistoryLimitand.spec.failedJobsHistoryLimitin your CronJob spec to retain only a fixed number of old Job objects. Also, combining history limits with ttlSecondsAfterFinished yields a comprehensive life cycle policy for scheduled workloads.
Frequently Asked Questions (FAQs)
Below are answers to the most common questions about removing Deployments in Kubernetes.Can I Recover a Deleted Deployment?
Kubernetes doesn’t provide a native “undo” for kubectl delete deployment. Once you delete a Deployment, its object and history are removed from etcd. To recover, you must reapply your original manifest or Helm chart from version control, or restore from etcd or Velero backups taken before the deletion.Does Delete Remove Related PVCs?
No, deleting a Deployment does not delete its PersistentVolumeClaims (PVCs) or the underlying PersistentVolumes (PVs) by default. PVCs are separate API objects and must be removed explicitly, such askubectl delete pvc <name>, if you want to free the storage.
Which Is Better: Delete vs. Suspend/Pause?
Delete (kubectl delete deployment) permanently removes the Deployment object, its ReplicaSets, Pods, and metadata. Use this for a full teardown. Pause (kubectl rollout pause deployment/<name>) halts the Deployment’s controller reconciliation but leaves the Deployment, ReplicaSets and Pods intact. It’s ideal for batching updates or maintenance.
How To Remove a Deployment in Kubernetes: Conclusion
Removing a Deployment in Kubernetes is more than just issuing a delete command. It’s about choosing the right approach for your use case, ensuring you have the necessary tools and permissions in place, and understanding the cleanup processes that run behind the scenes. Whether you opt to scale down or delete outright, a step-by-step removal guide helps you avoid orphaned resources and maintain cluster hygiene. Automating cleanup with flags like--cascade and --prune, and leveraging features such as ttlSecondsAfterFinished for Jobs, ensures your cluster stays in sync with your declared state without manual intervention. By applying these best practices you can keep your Kubernetes environments lean, reliable, and ready for whatever workload comes next.
Learn how to build a Kubernetes Operator from scratch to automate complex application life cycle tasks.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.