Last active
May 5, 2021 00:26
-
-
Save jonico/782fbb050ef9b89dacb4c2908d2bb950 to your computer and use it in GitHub Desktop.
Delete all package versions of a package of certain GitHub repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script that works to delete private packages stored on Github Packages | |
# | |
# Script is based on the work of Troy Fontaine (github.com/troyfontaine) | |
GITHUB_TOKEN=$GPR_PAT | |
REPO_OWNER=$1 | |
REPO_NAME=$2 | |
PACKAGE_NAME=$3 | |
if [ "$#" -ne 3 ]; then | |
echo "Usage: $0 <REPO_OWNER> <REPO_NAME> <PACKAGE_NAME>" >&2 | |
exit 1 | |
fi | |
graphqlJson() { | |
local query="$1"; shift | |
curl -s -H "Authorization: bearer $GITHUB_TOKEN" -X POST -H "Accept: application/vnd.github.v3+json" -d '{"query":"'"$query"'"}' 'https://api.github.com/graphql' | |
} | |
graphqlDelete() { | |
local query="$1"; shift | |
curl -s -H "Accept: application/vnd.github.package-deletes-preview+json" -H "Authorization: bearer $GITHUB_TOKEN" -X POST -d '{"query":"'"$query"'"}' 'https://api.github.com/graphql' | |
} | |
deletePackageVersion() { | |
PACKAGE_ID="$1" | |
local query="$(cat <<EOF | sed 's/"/\\"/g' | tr '\n\r' ' ' | |
mutation { | |
deletePackageVersion( | |
input:{packageVersionId:"$PACKAGE_ID"} | |
) | |
{ success } | |
} | |
EOF | |
)" | |
RESPONSE=$(graphqlDelete "$query") | |
echo "$RESPONSE" | |
} | |
listPackageVersions() { | |
local query="$(cat <<EOF | sed 's/"/\\"/g' | tr '\n\r' ' ' | |
query { | |
repository(name: "$REPO_NAME", owner: "$REPO_OWNER") { | |
id | |
name | |
registryPackages(first: 1, name: "$PACKAGE_NAME") { | |
nodes { | |
versions(first: 100) { | |
nodes { | |
id | |
version | |
} | |
} | |
name | |
id | |
} | |
} | |
} | |
} | |
EOF | |
)" | |
ID_LIST=$(graphqlJson "$query" | jq -r '.data.repository.registryPackages.nodes[].versions.nodes[].id') | |
} | |
purgePackageVersions() { | |
for ID in $ID_LIST | |
do | |
echo -e "Purging package version with ID: '$ID' ..." | |
deletePackageVersion $ID; | |
done | |
} | |
listPackageVersions | |
purgePackageVersions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
delete-all-package-versions.sh