Skip to content

Instantly share code, notes, and snippets.

@siketyan
Last active March 20, 2023 04:28
Show Gist options
  • Save siketyan/104ffd7bc1cf4290a9f1c48195910257 to your computer and use it in GitHub Desktop.
Save siketyan/104ffd7bc1cf4290a9f1c48195910257 to your computer and use it in GitHub Desktop.
Deletes all stale branches on GitHub (or Enterprise Server) using gh CLI and jq
set owner "siketyan"
set repo "foobar"
set last_committed_before "2023-03-01"
for name in (gh api "repos/$owner/$repo/branches?per_page=100" | jq -r .[].name)
echo "Checking $name"
set stale (gh api "repos/$owner/$repo/branches/$name" | jq -r "select(.commit.commit.committer.date < $last_committed_before) | .name")
if test -n "$stale"
echo "Branch $stale is stale, deleting"
gh api -X DELETE "repos/$owner/$repo/git/refs/heads/$stale"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment