Skip to content

Instantly share code, notes, and snippets.

@koron
Created August 21, 2020 02:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koron/c99ff2c818ee6ffee1e59edcb67be2e9 to your computer and use it in GitHub Desktop.
Save koron/c99ff2c818ee6ffee1e59edcb67be2e9 to your computer and use it in GitHub Desktop.
Shell script to delete all artifacts from GitHub Actions
#!/bin/bash
#
# Delete all artifacts
#
# USAGE: ./delete-all-artifacts.sh {YOUR_PERSONAL_ACCESS_TOKEN} {OWNER_REPO}
#
# Where `{YOUR_PERSONAL_ACCESS_TOKEN}` should have `workflow` and `repo` scopes
# to work this script. `public_repo` can be used instead of `repo`, when you
# want delete artifacts from public repositories only.
#
# Where `{OWNER_REPO}` should `{user}/{reponame}` style identifier of a
# repository. Example: `koron/foobar`
set -eu
token=$1 ; shift
ownerrepo=$1 ; shift
invoke_api() {
curl -s -H "Authorization: token ${token}" -H "Accept: application/vnd.github.v3+json" "$@"
}
list_artifacts() {
echo "List artifacts" 1>&2
invoke_api https://api.github.com/repos/${ownerrepo}/actions/artifacts
}
delete_artifact() {
u=$1 ; shift
echo "Delete artifact: $u" 1>&2
invoke_api -X DELETE "$u"
}
while true ; do
sleep 2
list=$(list_artifacts | awk -F '"' '/"url":/{print $4}')
if [ x"${list}" == x ] ; then
echo Finished.
exit 0
fi
for u in ${list} ; do
sleep 0.1
delete_artifact "$u"
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment