Deleting images from Github Package Registry

As part of my CI Actions workflow, I push a docker image of the latest branch commit to GPR and pull it onto a remote docker-machine for testing. The image is tagged with the commit sha like so to prevent concurrent workflows from mixing:

docker.pkg.github.com/$GITHUB_REPOSITORY/$REPOSITORY_NAME:$GITHUB_SHA

After my tests are complete, I would like to delete/remove the image from Github Package Registry but I can’t seem to find the functionality after reviewing the docker x gpr documentation here: https://help.github.com/en/github/managing-packages-with-github-packages/configuring-docker-for-use-with-github-packages or package management documentation here: https://help.github.com/en/github/managing-packages-with-github-packages/deleting-a-package.

Is there a way to delete/remove docker images from GPR? If so can someone point me in the right direction or does this functionality not exist yet?

7 Likes

Hi @kachow6,

Thank you for being here! This functionality is currently in the works, for now you might message us via  https://github.com/contact with the info, for our support folks to take care of it.

3 Likes

Hi thanks for the response @andreagriffiths11,

Where can I find the update/release log for Github Package Registry? I would like to be notified when this feature is released.

4 Likes

Hi @andreagriffiths11 ! Any update on this topic yet? 

Best regards, Max

4 Likes

Just wanted to chime in here.  I want to use the image registry for my team since it will speed up our development efforts having the builds so close to the source, but not being able to manage the images with full CRUD really diminishes our desire to use it.

Without this abililty I’m going to have to make a choice soon to use a service like ECR to manage our images.  This doesn’t make much sense since we’re already paying for an enterprise account.  Once we make a choice to use an external service across all of our applications, migrating back to GHR won’t make much sense because of the manhours it would take to migrate everything back to yet another image registry.

I’m not asking for an explanation as to why it’s not here, but I can’t wait forever for this functionality and I assume there are a couple thousand other teams that are in the same situation.  That seems like a fairly heavy loss of untapped revenue, enough that perhaps this could have a priority escalation or at the very least far more communcation about the status of the improvement.

Thanks,

Jake

11 Likes

We also have the same issues. We would like to delete images and we’d be glad if that was supported soon.

6 Likes

https://help.github.com/en/packages/publishing-and-managing-packages/deleting-a-package

Since initially making this post I was able to come up with a solution using curl, jq, and the github graphql api that worked for my specific use case (deleting a gpr image based on the version name within an actions workflow). Sharing it here in hopes that it helps someone else. Note, I had to create a github personal access token with read, write, and delete privileges for GPR packages, which i stored as a repository secret.

Get the 10 most recent package versions and save the version id with name that matches the current commit sha:

- name: get gpr package version id from first 10 packages
      run: |
        echo "::set-env name=VERSION_ID::$(curl -X POST \
        -H "Accept: application/vnd.github.package-deletes-preview+json" \
        -H "Authorization: Bearer ${{ secrets.GPR_PERSONAL_ACCESS_TOKEN }}" \
        -d "{\"query\":\"query {repository(owner:\\\""$YOUR_ORG_NAME"\\\", name:\\\""$REPOSITORY_NAME"\\\") {registryPackages(first:10) {edges{node{id, name, versions(first:10){edges {node {id, version}}}}}}}}\"}" \
        https://api.github.com/graphql | jq '.data.repository.registryPackages.edges[].node.versions.edges[].node' | jq -s '.' | jq --arg GITHUB_SHA $GITHUB_SHA '.[] | select(.version==$GITHUB_SHA) | .id')"

Attempt to delete the version based on $VERSION_ID:

- name: delete gpr package version
      id: attempt_delete
      run: |
        echo "::set-output name=delete::$(curl -X POST \
        -H "Accept: application/vnd.github.package-deletes-preview+json" \
        -H "Authorization: Bearer ${{ secrets.GPR_PERSONAL_ACCESS_TOKEN }}" \
        -d "{\"query\":\"mutation { deletePackageVersion(input:{packageVersionId:\\\"${{ env.VERSION_ID }}\\\"}) { success }}\"}" \
        https://api.github.com/graphql)"

Check result of deletion step and fail workflow if deletion failed (otherwise passes regardless):

- name: check if delete was successful
      run: |
        if [-n $(echo "$DELETE_RESULT" | jq '.data.deletePackageVersion')]
          then
          echo "$DELETE_RESULT" | jq '.errors' | jq '.[].message'
          exit 1
        fi
      env:
        DELETE_RESULT: ${{ steps.attempt_delete.outputs.delete }}
3 Likes

Thanks so much for this. I tag my Docker images with the branch name - for anyone doing something similiar, I was able to modify the first query to get the version ID like this:

- name: Get Docker image's GPR ID
        run: |
          echo "::set-env name=VERSION_ID::$(curl -X POST \
          -H "Accept: application/vnd.github.packages-preview+json" \
          -H "Authorization: Bearer ${{ github.token }}" \
          -d "{\"query\":\"query {repository(owner:\\\""<org name>"\\\", name:\\\""<repo name>"\\\") {packages(names:[\\\""<package name>"\\\"], first:1) {edges{node{id, name, version(version:\\\""${{ github.event.pull_request.head.ref }}"\\\"){id, version}}}}}}\"}" \
          https://api.github.com/graphql | jq '.data.repository.packages.edges[].node.version.id')"

If you’re looking for a simpler delete solution we maintain an action that helps you automate this process https://github.com/actions/delete-package-versions

1 Like

Updating this to be the recommended solution thanks

2 Likes

Updated script to get first 10 packages after registryPackages was deprecated on 2020-04-01:

- name: get gpr package version id from first 10 packages
      run: |
        echo "::set-env name=VERSION_ID::$(curl -X POST \
        -H "Accept: application/vnd.github.package-deletes-preview+json" \
        -H "Authorization: Bearer ${{ secrets.GPR_PERSONAL_ACCESS_TOKEN }}" \
        -d "{\"query\":\"query {repository(owner:\\\""$YOUR_ORG_NAME"\\\", name:\\\""$REPOSITORY_NAME"\\\") {packages(first:10) {edges{node{id, name, versions(first:10){edges {node {id, version}}}}}}}}\"}" \
        https://api.github.com/graphql | jq '.data.repository.packages.edges[].node.versions.edges[].node' 

name: Get Docker image’s GPR ID
run: |
echo “::set-env name=VERSION_ID::(curl -X POST \ -H "Accept: application/vnd.github.packages-preview+json" \ -H "Authorization: Bearer {{ github.token }}”
-d “{“query”:”" <a href="“https://thegta5android.com/”">click to download gta 5
click here "
:\"""\", name:\"""\") {packages(names:[\"""\"], first:1) {edges{node{id, name, version(version:\""${{ github.event.pull_request.head.ref }}"\"){id, version}}}}}}"}"
https://api.github.com/graphql | jq ‘.data.repository.packages.edges.node.version.id’)"