2026年9月4日金曜日

GCP の ArtifactRegistry にある latest タグ以外のイメージを一括で削除する方法

GCP の ArtifactRegistry にある latest タグ以外のイメージを一括で削除する方法

概要

CLI を使いましょう

環境

  • macOS 26.6.2
  • gcloud cli 545.0.0

タグの一覧を表示

gcloud artifacts docker images list \
  asia-northeast1-docker.pkg.dev/product-123456/gae-standard/app/default \
  --include-tags --format='get(version,tags)'

対象のタグに絞り込む

gcloud artifacts docker images list \
  asia-northeast1-docker.pkg.dev/product-123456/gae-standard/app/default \
  --include-tags \
  --format='get(version,tags)' |
while read -r version tags; do
  if [[ "$tags" != *latest* ]]; then
    echo ${tags}
    echo ${version}
  fi
done

絞り込んだイメージを削除

gcloud artifacts docker images list \
  asia-northeast1-docker.pkg.dev/product-123456/gae-standard/app/default \
  --include-tags \
  --format='get(version,tags)' |
while read -r version tags; do
  if [[ "$tags" != *latest* ]]; then
    echo ${tags}
    echo ${version}

    gcloud artifacts docker images delete \
      "asia-northeast1-docker.pkg.dev/product-123456/gae-standard/app/default@$version" \
      --delete-tags \
      --quiet
  fi
done

最後に

プロジェクトのIDやリポジトリ名やパッケージ名は適宜変更してください

0 件のコメント:

コメントを投稿