Kubernetes
Resource Management
Cluster Cleanup
Kubernetes Commands
DevOps

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.

Practice system design

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:

bash
1# Delete all Pods in the namespace
2kubectl delete pods --all
3
4# Delete all Services in the namespace
5kubectl delete services --all
6
7# Delete Deployments, StatefulSets, etc.
8kubectl delete deployments --all
9kubectl delete statefulsets --all
10
11# Delete entire namespaces (including all resources within)
12kubectl delete namespace <namespace-name>

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:

  1. View All Available Namespaces:
bash
   kubectl get namespaces
  1. Delete All Resources from a Specific Namespace:
bash
   kubectl delete all --all -n <namespace-name>
  1. Delete the Entire Namespace and its Resources: This removes the namespace itself along with all the resources within it.
bash
   kubectl delete namespace <namespace-name>
  1. Delete all namespaces: This will effectively delete all resources in the entire cluster.
bash
   kubectl delete namespace --all

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:
bash
  kubectl config current-context
  • List All Contexts:
bash
  kubectl config get-contexts
  • Switch Contexts:
bash
  kubectl config use-context <context-name>

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):

bash
1#!/bin/bash
2
3# Loop through all namespaces and delete each one, excluding essential namespaces
4for namespace in $(kubectl get namespaces -o jsonpath='{.items[*].metadata.name}')
5do
6  if [[ "$namespace" == "kube-system" || "$namespace" == "kube-public" || "$namespace" == "kube-node-lease" || "$namespace" == "default" ]]
7  then
8    echo "Skipping deletion of essential namespace: $namespace"
9  else
10    echo "Deleting namespace: $namespace"
11    kubectl delete namespace $namespace
12  fi
13done

Summary Table

TaskCommand
Delete All Podskubectl delete pods --all
Delete All Serviceskubectl delete services --all
List All Namespaceskubectl get namespaces
Delete All Resources in Namespacekubectl delete all --all -n <namespace>
Delete Namespacekubectl delete namespace <namespace>
Delete All Namespaceskubectl delete namespace --all
Current Contextkubectl config current-context
Switch Contextkubectl config use-context <context-name>
Backup ResourcesEnsure 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.