Home > Back-end >  How to list Kubernetes namespace in a single line, and delete them
How to list Kubernetes namespace in a single line, and delete them

Time:07-21

I would like to output all namespaces in a single line to a variable so I can delete the namespaces later.

Example:

TOBEDELETED=$(kubectl get namespace -o=name | grep "SOME_NAME")

eval $(kubectl delete namespace ${TOBEDELETED})

CodePudding user response:

An easy way is to pipe the output of your grep to tr "\n" " " to replace all newlines with spaces. Also, your eval is unnecessary.

TOBEDELETED=$(kubectl get namespace -o=name | grep "SOME_NAME" | tr "\n" " ")
kubectl delete namespace ${TOBEDELETED}
  • Related