diff --git a/.buildkite/scripts/pipelines/pipeline_prune_staging_docs.sh b/.buildkite/scripts/pipelines/pipeline_prune_staging_docs.sh index 2fa808b3302..b66f377b93c 100644 --- a/.buildkite/scripts/pipelines/pipeline_prune_staging_docs.sh +++ b/.buildkite/scripts/pipelines/pipeline_prune_staging_docs.sh @@ -25,14 +25,17 @@ BUCKET=${GPROJECT}-${EUI_DOCS_PROJECT} # https://cloud.google.com/storage/docs/gsutil/commands/ls ls_options=( -d # only list directories - -l # Print additional details about the subdir + -l # include additional details about the subdir, notably the date as a second field ) -echo "Listing all PR staging links" -gsutil ls "${ls_options[@]}" "gs://${BUCKET}/pr_*" # | sort -k 2 # sort by the 2nd field returned by -l which is the 'created by' timestamp - -# https://cloud.google.com/storage/docs/gsutil/commands/rm -rm_options=( - -r # recursive, delete everything inside subdir - -m # enables multi-threading for large numbers of objects +echo "Getting all but the most recent 50 PR staging links..." +list=$(gsutil ls "${ls_options[@]}" "gs://${BUCKET}/pr_*" \ + | sort -k 2 `# sort by the 2nd field returned by -l which is a timestamp` \ + | head -n -50 `# remove the last 50 items, so basically keep the latest 50 staging docs` \ ) -# gsutil rm "${rm_options[@]}" "gs://${BUCKET}/pr_TODO" +while IFS= read -r line || [[ -n $line ]]; do + url="$(echo -e "${line}" | tr -d '[:space:]')" # trim the leading whitespaces + url=${url%/} # trim the trailing slash + echo "Deleting $url" + # https://cloud.google.com/storage/docs/gsutil/commands/rm + gsutil -m rm -r "$url" +done < <(printf '%s' "$list")