Exporting and Deleting GitHub Gists
Published on 2023-03-15
As part of my (ongoing) effort to leave GitHub (owned by Microsoft) I wanted to export my account data and delete all repositories and gists. By leaving GitHub I mean that I won't be using it anymore for any development, but keep the account for now to contribute to projects that are currently unable to leave GitHub for whatever reason.
GitHub's account export does not include GitHub Gist for some reason. Luckily they have an API that can be used to fetch, i.e. clone the gists, and delete them quite easily.
The script below will fetch a list of your gists, clone the repository and then delete it. If you have many gists you need to run it multiple times as it does not implement pagination. We avoid needing that by simply deleting the gists after fetching them.
In order to use it, set API_TOKEN
to your own token. You can get one through
personal access token (classic). Make
sure you give it the gist
scope.
#!/bin/sh
API_TOKEN=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
GIST_REPO_LIST=$(curl -s -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${API_TOKEN}" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/gists | jq .[].git_pull_url -r)
for GIST_REPO in ${GIST_REPO_LIST}; do
# clone
GIST_DIR=$(basename ${GIST_REPO})
if ! [ -d ${GIST_DIR} ]; then
git clone --bare ${GIST_REPO}
fi
# delete
GIST_ID=$(basename ${GIST_REPO} .git)
curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${API_TOKEN}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/gists/${GIST_ID}
done