How to delete all resources from Kubernetes one time?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In managing Kubernetes clusters, there are scenarios where it's necessary to delete all resources, such as during cluster teardown or when redeploying applications from scratch. Below is a comprehensive guide on how to efficiently and safely delete all resources from a Kubernetes cluster at once.
Table of Contents
- Understanding Kubernetes Resources
- Commands to Delete Resources
- Using Namespaces for Deletion
- Utilizing Kubernetes Contexts
- Important Precautions
- Example Script for Deletion
- Summary Table
Understanding Kubernetes Resources
Kubernetes resources include a wide range of objects such as Pods, Services, Deployments, StatefulSets, ConfigMaps, Secrets, and more. These resources are defined in YAML or JSON files and managed via the Kubernetes API using tools like kubectl.
Commands to Delete Resources
To delete resources, the primary command used is kubectl delete. Below are the commands to remove different types of resources:
The --all flag is crucial, as it indicates deletion of all resources of the specified type within the namespace.
Using Namespaces for Deletion
Namespaces can be a powerful tool when managing resource deletion:
- View All Available Namespaces:
- Delete All Resources from a Specific Namespace:
- Delete the Entire Namespace and its Resources: This removes the namespace itself along with all the resources within it.
- Delete all namespaces: This will effectively delete all resources in the entire cluster.
Utilizing Kubernetes Contexts
Kubernetes contexts store cluster connection information for easier management. This is useful when simultaneously managing multiple clusters or environments.
- View Current Context:
- List All Contexts:
- Switch Contexts:
Switching contexts ensures you’re deleting resources from the intended cluster, avoiding accidental deletions.
Important Precautions
- Backups: Always back up critical data stored in ConfigMaps, Secrets, and Persistent Volumes before deletion.
- Check Permissions: Ensure you have necessary permissions for deleting cluster-wide resources.
- Production Environments: Validate the impact of deletions on production environments and opt for staged deletions or rollbacks if possible.
Example Script for Deletion
Below is a sample Bash script that deletes all namespaces except for a few essential ones (kube-system, kube-public, kube-node-lease, and default):
Summary Table
| Task | Command |
| Delete All Pods | kubectl delete pods --all |
| Delete All Services | kubectl delete services --all |
| List All Namespaces | kubectl get namespaces |
| Delete All Resources in Namespace | kubectl delete all --all -n <namespace> |
| Delete Namespace | kubectl delete namespace <namespace> |
| Delete All Namespaces | kubectl delete namespace --all |
| Current Context | kubectl config current-context |
| Switch Context | kubectl config use-context <context-name> |
| Backup Resources | Ensure backups are created for ConfigMaps, Secrets, etc. |
By understanding these methods and precautions, administrators can manage their Kubernetes clusters effectively and ensure that resources are safely deleted when required.
Related reading
- How to delete completed kubernetes pod?
- How to Delete Kubernetes Service
- How to delete node in EKS managed node group if the Kubelet crashes or stops reporting?
- How to delete persistent volumes in Kubernetes
- How to delete images from a private docker registry?
- How to delete non empty s3 bucket with terraform?
- how to delete tiller from kubernetes cluster
- how to delete/remove calico cni from my kubernetes cluster

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.