Skip to main content

Command Palette

Search for a command to run...

I Accidentally Deleted a Kubernetes Namespace

Published
3 min read
I Accidentally Deleted a Kubernetes Namespace
P

Recent Computer Science graduate passionate about DevOps and cloud technologies. Demonstrated ability to build scalable systems through hands-on projects achieving 70% faster deployments using CI/CD pipelines, Kubernetes, and GitOps workflows. Strong foundation in full-stack development with practical experience in AWS, Docker, and security-first automation. Eager to apply DevSecOps expertise and problem-solving skills to build reliable, efficient infrastructure at scale.

While working on my DevOps project, I mistakenly deleted a Kubernetes namespace.
It was just a test setup, but it made me think:

What if this had happened in production?

Deleting a namespace removes everything inside it — services, pods, secrets, deployments.
In production, this one mistake can bring the entire system down.

That small incident taught me a big lesson about how careful we must be with Kubernetes in real environments.
In this article, I will explain what happens when a namespace is deleted, the risks involved, and how to prevent such failures.


WHAT HAPPENS INSIDE KUBERNETES

Step 1: Namespace Marked for Deletion

kubectl get ns payments -o yaml
status:
  phase: Terminating

Once you delete a namespace, Kubernetes does not remove it immediately. It is first marked as Terminating.


Step 2: All Resources Are Marked for Deletion (Cascade Delete)

Kubernetes performs a cascading delete, meaning everything inside the namespace is scheduled for deletion.

Resources affected:

  • Pods

  • Deployments

  • ReplicaSets

  • Services

  • ConfigMaps

  • Secrets

  • Ingress

  • HPA (HorizontalPodAutoscaler)

  • PDB (PodDisruptionBudget)

At this point, workloads start disappearing and traffic begins to fail.


Step 3: Finalizers Slow Everything Down

Finalizers are special hooks that block deletion until cleanup is completed.

Create a sample namespace and workload

kubectl create ns payments
kubectl create deployment web --image=nginx -n payments
kubectl expose deployment web --port=80 -n payments
kubectl get all -n payments

Check for finalizers

kubectl get pods -n payments -o yaml | grep finalizers

Example ConfigMap with a finalizer:

apiVersion: v1
kind: ConfigMap
metadata:
  name: prod-config
  namespace: payments
  finalizers:
    - kubernetes
data:
  env: prod

Apply the finalizer:

kubectl apply -f finalizer.yaml

Now delete the namespace:

kubectl delete ns payments
kubectl get ns payments
kubectl get all -n payments
kubectl get pods -n payments

Namespace stays in Terminating state until all finalizers are cleared.


WHAT USERS EXPERIENCE

For Users

  • 502 / 503 errors

  • Blank pages

  • Payment failures

  • Application unavailable

For Monitoring & Observability

  • Error rate spikes

  • Latency (p99) explodes

  • Alerts firing non-stop

CAN ARGOCD SAVE YOU?

Scenario 1: Namespace Is Git-Managed

If ArgoCD manages the namespace:

kind: Namespace
metadata:
  name: payments

What happens?

  • ArgoCD recreates the namespace

  • Resources inside are still gone


Scenario 2: ArgoCD Auto-Sync Enabled

  • Namespace recreated

  • Deployments recreated

  • Pods start coming up

    But problems remain:

  • Secrets may be missing

  • PVCs may not bind

  • External resources are permanently lost


HOW DO YOU RECOVER (REAL WORLD)

Recovery Option 1: GitOps Redeploy (Best Case)

Steps:

kubectl create ns payments
argocd app sync payments-app

Then:

  • Verify workloads

  • Recreate secrets

Works only if:

  • Everything is defined in Git

  • Secrets are external (Vault, AWS Secrets Manager)


Recovery Option 2: PVC & Data Recovery

If PVCs were deleted:

  • Data is GONE

Only backups can help:

  • EBS snapshots

  • CSI snapshots

  • Backup tools (Velero)

Hard truth:

Kubernetes does NOT back up your data by default.


Recovery Option 3: Manual Recreation (Worst Case)

  • Recreate secrets

  • Reconfigure ingress

  • Reattach DNS

  • Restart external integrations

    This can take hours during an outage.


HOW TO PREVENT THIS FOREVER

1: Use RBAC Properly

Allow only read access:

verbs: ["get", "list"]

No delete access for humans in prod


2: Disable Namespace Deletion

Use admission controllers:

  • OPA Gatekeeper

  • Kyverno

Example rule:

Block delete namespace if label = prod


3: Separate kubeconfig Contexts

kubectl config use-context prod

Best practices:

  • Red terminal theme

  • Loud prompt (⚠️ PROD ⚠️)


4: Golden SRE Rule

🚫 Humans should NOT have delete access in production.

Only automation. Only GitOps.