Search code examples
kuberneteskubectl

Shell Script for delete the namespace if it is existed and waited till it complete


script will need to check if aaa-new-ui-dev and bbb-java-new-ui-dev namespace exist - and if it exists - it needs to delete them and wait until delete operation is complete

I am trying to create a shell script which check if namespace is exist and if it exists then it should delete the kubectl namespace.


Solution

  • # For each namespace to delete.
    for ns in aaa-new-ui-dev bbb-java-new-ui-dev ; do 
      # If 'get' returns 0, then the namespace exists. 
      if kubectl get namespace/$ns ; then
        # Issue delete. 
        kubectl delete namespace/$ns
        # Wait up to 30 seconds for deletion.
        kubectl wait --for=delete namespace/$ns --timeout=30s
      else
        # Get returned an error. Assume namespace does not exist.
        echo "$project does not exist; skipping delete"
      fi
    done