Deleting all BigQuery datasets across multiple projects

Mon, 01 July 2019

delete-all-bigquery-datasets-across-projects.sh
#!/usr/bin/env sh

PROJECTS="my-first-project-id
my-second-project-id
my-third-project-id
my-fourth-project-id"


COUNTER=0
for PROJECT_ID in ${PROJECTS}
do
  echo "${PROJECT_ID}"
  SINGLE_SETS=`bq ls --project_id="${PROJECT_ID}"`
  for DATASET in ${SINGLE_SETS}
  do
    if [[ ${DATASET} == *"datasetId"* ]]; then
      echo "                  - Skipped ${DATASET}"
    else
        if [[ ${DATASET} == *"----"* ]]; then
            echo "                  - Skipped ${DATASET}"
        else
            COUNTER=$((COUNTER+1))
            FULL_NAME="${PROJECT_ID}:${DATASET}"
            echo "Deleting ${FULL_NAME}"
            DELETED=`bq rm -r -f ${FULL_NAME}`
            echo " - Progress - ${COUNTER} deleted so far"
        fi
    fi
  done
done

echo "${COUNTER} Deleted total"