Set expiration table on existing Bigquery tables

PROJECT_ID="my_gcp_project"
DATASET_ID="my_big_query_dataset"

# list tables in the dataset
#
# bq ls --project_id $PROJECT_ID $DATASET_ID
# list objects in the dataset. Eg tables and views. returns a table shaped text
#
# grep TABLE
# keep lines of type table
#
# awk '{print $1}'
# keep the first column
#
# tail -n +3
# keep only 3rd line and onwards
for table in $(bq ls --project_id $PROJECT_ID $DATASET_ID | grep TABLE | awk '{print $1}'| tail -n +3); do
  echo "Setting expiration for table: $table"
  # Set the expiration time to 90 days (7776000 seconds)
  bq update --expiration 7776000 $PROJECT_ID:$DATASET_ID.$table
done

Send a Comment

Your email address will not be published.