Bash scripting to ensure Kubernetes resources are deleted in synchronization

Sometimes we need to ensure that resources in Kubernetes are fully deleted before we setup other resources. In Kubernetes the timing and the synchronization can be very import and relevant.

In that blog post we see a function of a bash script that exactly does that job for namespaces. We are using a “for loop” combined with a nested “while loop” and other functionalities in bash to address that topic.

The blog post is organized in following sections:

  1. Example scenario
  2. Used bash scripting functionality
  3. Filter result values from a kubectl command
  4. Bash function code
  5. Summary

1. Example scenario

In that example we verify three different namespaces in a Kubernetes cluster, if they are deleted for two times. If they are deleted we move on with the remaining bash script and if they are not deleted we will stop the script execution.

2. Used bash scripting functionality

In our bash script “function” we are using a “for loop” combined with “an array” and a nested “while loop“.

These nested loops containing “if .. else” statements in combination of “string compare” with “variables” and an “exit” statement to end the script execution and we use a “break” statement to continue in the “function“, if the namespaces were successfully deleted.

Also we are using “bash pipes” in combination “assign output to a variable” to get the needed value from a terminal output.

List of used bash functionality:

3. Filter result values from a kubectl command

To filter the terminal output we use “assign output to a variable” in combination with a “grep” command for regular expressions and an awk command which is also for regular expressions to get the right value from the terminal output created by our kubectl command.

Example output of the used kubectl command

kubectl get namespace default
default   Active   74d

4. Bash function code

Here is the full function:

function verifyDeletion () {

  export max_retrys=2
  array=("cert-manager" "olm" "operators")
  export STATUS_SUCCESS=""
  for i in "${array[@]}"
        echo ""
        echo "------------------------------------------------------------------------"
        echo "Check $i"
        export FIND=$i
        while :
           STATUS_CHECK=$(kubectl get namespace $FIND | grep $FIND | awk '{print $2;}')
           echo "Status: $STATUS_CHECK"
           if [ "$STATUS_CHECK" = "$STATUS_SUCCESS" ]; then
                echo "$(date +'%F %H:%M:%S') Status: $FIND is deleted"
                echo "------------------------------------------------------------------------"
            elif [[ $j -eq $max_retrys ]]; then
                echo "$(date +'%F %H:%M:%S') Please run '' first!"
                echo "$(date +'%F %H:%M:%S') Prereqs aren't ready!"
                echo "------------------------------------------------------------------------"
                exit 1              
                echo "$(date +'%F %H:%M:%S') Status: $FIND($STATUS_CHECK)"
                echo "------------------------------------------------------------------------"
            sleep 3

5. Summary

It is function is very useful and a good starting point for other nested loops related to bash automation for Kubernetes kubectl commands.

I hope this was useful to you and let’s see what’s next?



#bash, #bashscripting, #kubernetes

3 thoughts on “Bash scripting to ensure Kubernetes resources are deleted in synchronization

Add yours

  1. Hi Thomas, Thanks for the brief explanation. In your verifyDeletion function you mentioned about BUT where is that bash script in this blog post or GitHub project?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at

Up ↑

%d bloggers like this: