Skip to content

Instantly share code, notes, and snippets.

@silashansen
Created August 8, 2024 13:51
Show Gist options
  • Select an option

  • Save silashansen/a3da10144225611bd50d75baab9df869 to your computer and use it in GitHub Desktop.

Select an option

Save silashansen/a3da10144225611bd50d75baab9df869 to your computer and use it in GitHub Desktop.
Delete image from repository
This is confirmed working with Harbor v2.0.0
# Parameters ------------------------
HARBOR_URL="https://reg.registry.dk"
HARBOR_USER="username"
HARBOR_PASSWORD="secret"
PROJECT="project"
REPOSITORY="repository"
TAG="0.0.1"
# -----------------------------------
#Construct image name
IMAGE_NAME="${PROJECT}/${REPOSITORY}"
# Get JWT token from registry
TOKEN=$(curl -u "${HARBOR_USER}:${HARBOR_PASSWORD}" \
"${HARBOR_URL}/service/token?service=harbor-registry&scope=repository:${IMAGE_NAME}:*" | jq -r .token)
# Get digest for the tag
DIGEST=$(curl -sSL -I -H "Authorization: Bearer $TOKEN" \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
"${HARBOR_URL}/v2/${IMAGE_NAME}/manifests/${TAG}" \
| awk '$1 == "docker-content-digest:" { print $2 }' \
| tr -d $'\r')
# Delete the image
curl -sSL -H "Authorization: Bearer $TOKEN" -X DELETE \
"${HARBOR_URL}/v2/${IMAGE_NAME}/manifests/${DIGEST}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment