How to delete all releases and tags in a github repository

1.7k views Asked by At

I tried the github cli:

gh release list | sed 's/|/ /' | awk '{print $1, $8}' | while read -r line; do gh release delete -y "$line"; done

as described here: https://dev.to/dakdevs/delete-all-releases-from-github-repo-13ad

But it only works for releases where the name equals the tag. As soon as a name has spaces in it, awk fails to separate the columns properly.

maybe the gh cli output changed since the article was written or awk on macos has different defaults?

2

There are 2 answers

0
Jonas Eicher On

Adding -F '\t' helped awk to separate the columns correctly. This command deletes all gh releases:

gh release list | awk -F '\t' '{print $3}' | while read -r line; do gh release delete -y "$line"; done

You can also delete the tags with the following flag:

--cleanup-tag   Delete the specified tag in addition to its release
0
Dave Yarwood On

I happened to need to do this exact thing (delete all of the releases in a repo), and I was able to use cut for this successfully. Here's the command I ran:

gh release list --limit 500 \
  | cut -f3 \
  | while read release_tag; do
  gh release delete -y "$release_tag"
done

This works because the gh output is tab-separated, and cut separates fields by tabs by default. So cut -f3 takes the third field in the output, which is the release tag.

Other notes:

  • The -y option makes it so that it doesn't ask you to confirm before deleting each release. Useful if you're confident that you know what you're doing and you have a lot of releases to delete.

  • The --limit 500 part is optional. The default page size is 30, so if you have more than 30 releases, you need to use the --limit option to make the page size bigger and fetch all of your releases, instead of just the first 30.

  • Like Jonas mentioned, you can include the --cleanup-tag option when deleting a release to also delete the tag.