March 14th, 2023

Github: Delete all repository deployments

Sometimes you just want to delete all your deployments

# This script deletes all GitHub Actions runs for the current repository.
# It is useful for cleaning up the Actions tab after a large number of runs
# have been created, such as when testing a workflow.
# check for help flag (-h or --help)
if [[ $1 == "-h" || $1 == "--help" ]]; then
	echo "Usage: $0 [count]"
	echo "Deletes all GitHub deployments for the current repository."
	echo "The count argument is optional and defaults to 10."
	exit 0
# default limit value: 10
# parse limit value
if ! [[ $limit =~ ^[0-9]+$ ]]; then
	echo "Invalid limit value: $limit"
	exit 2
# transform $limit as number
limit=$(echo "$limit" | tr -d '[:space:]')
# check if gh is installed
if ! command -v gh &>/dev/null; then
	echo "gh could not be found. Please install it from"
	exit 1
# check if gh is authenticated
if ! gh auth status &>/dev/null; then
	echo "gh is not authenticated. Please run 'gh auth login' to authenticate."
	exit 1
REPO=$(gh repo view --json nameWithOwner -q .nameWithOwner)
if [[ -z $repo ]]; then
	echo "No repository found."
	exit 1
# Fetch all deployments in the repository
deployments=$(gh api "repos/$REPO/deployments" --jq '.[].id')
if [[ -z $deployments ]]; then
	echo "No deployments found."
	exit 0
total_deployments=$(echo "$deployments" | wc -l)
# Loop through deployment IDs and delete each deployment
for deployment_id in $deployments; do
	printf "\r [%d/%d] Deleting deployment: %s..." "$completed" "$total_deployments" "$deployment_id"
	gh api -X DELETE "repos/$REPO/deployments/$deployment_id" --silent
printf "\nAll deployments have been deleted."

You might also like: