11

I'm trying to delete a Release in GitHub, so I'm doing

git tag -d {release-tag-name}
git push origin :{release-tag-name}

This removes the tag (local and remote) but it leaves a Draft release in GitHub that I also want to delete.

I can delete it by login into GitHub and clicking the delete button but I want to avoid doing this through the website.

Is there a command to achieve this? I've found some other similar postings about removing tags but they all end up going to GitHub to delete the Draft.

Edit

In this question's accepted answer Step 2 and 5 are related to my question. Step 2 says This will turn your "Release" on GitHub into a Draft that you can later delete. while step 5 instructs to delete the Draft in GitHub's site, not trough a command.

Community
  • 1
  • 1
jorgehmv
  • 3,633
  • 3
  • 25
  • 39

3 Answers3

9

Since @GabLeRoux pointed out that hub now includes the ability to delete releases, and it has the ability to list drafts and format the output as we wish, I wrote the following to delete all draft releases from our repo:

hub release -f "%T (%S) %n" --include-drafts | grep " (draft)" | awk '{print $1}' | xargs -t -n1 hub release delete

Breaking this down:

hub release -f "%T (%S) %n" --include-drafts: Prints all releases, and if they have a status like "draft" or "pre-release", put that status inside of parenthesis. You'll end up with output like this:

release-name-1 ()
release-name-2 ()
release-name-3 (draft)
release-name-4 ()
release-name-5 (draft)
release-name-6

Now pipe that into | grep " (draft)" so that we only print lines with (draft):

release-name-3 (draft)
release-name-5 (draft)

Now for each of those lines, pipe that into | awk '{print $1}' to take only the first part (before the first space). (This won't work if any releases have spaces in their name):

release-name-3
release-name-5

Now pipe all of those draft release names into xargs -t -n1 hub release delete, which will call hub release delete <RELEASE_NAME_HERE> for each line of input:

hub release delete release-name-3
hub release delete release-name-5

(As for those xargs params: -t echoes the command first, and -n1 tells it to make a separate call to hub for every line of input.)

If you want to go slow and see what draft releases it thinks it should delete, just run this portion:

hub release -f "%T (%S) %n" --include-drafts | grep " (draft)" | awk '{print $1}'

And if you want to go really slow, add -p as a param to xargs, and it will prompt you before running each command.

Taytay
  • 11,063
  • 5
  • 43
  • 49
  • 1
    Almost worked, for some reason when i run it, the "Draft" card's were still appearing, I deleted all tag with `git tag -d tagname` and then `git push origin :refs/tags/tagname` one by one and after running that command, most of them were gone just couple missing. – user1767754 Oct 26 '19 at 04:18
  • 1
    Good answer, I've just realized that with this method, you won't be able to delete untagged releases. Those can be releaes, which lost it's tags after doing a `tag -d tagname` – user1767754 Jan 06 '20 at 04:12
  • This should be the Answer. It works for me. You can use homebrew (mac) or choco (Windows) to install hub cli. https://hub.github.com/ – GunWanderer Jan 26 '23 at 21:09
8

Releases are not something git CLI can help you with.

Releases are GitHub specific thing.

You can use GitHub API to create/update/delete releases.

DELETE /repos/:owner/:repo/releases/:id

If you want to automate interaction with GitHub API you can do the following:

  1. Get an API token with proper permissions.
  2. Store it somewhere, let's say environment variables.
  3. Write a few scripts.

For example in this case you can have one script to delete local tag, call API to get tag id by name, delete remote tag and delete a release.

Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39
0

I needed to do this too. Anton Sizikov's answer is right but working with github's api directly may be a bit tricky sometimes.

hub had an issue on this, and the PR is now merged

To delete a release, all you need to do is install hub and run this inside your project's folder:

hub release delete <TAG>

You can delete all the releases with something like this

git fetch --all --tags
git tag | xargs hub release delete

Or you can list all the tags you want to delete in a file (one tag per line), then do this:

cat tags_i_want_to_delete.txt | xargs hub release delete

See tldr xargs for details

Alternative solution: working nodejs example to remove draft tags

Thanks to Steve Mao for github-remove-all-releases!

npm init
npm install --save github-remove-all-releases dotenv

main.js:

require('dotenv').config();

var githubRemoveAllReleases = require('github-remove-all-releases');

var AUTH = {
    type: 'oauth',
    token: process.env.GITHUB_TOKEN
};

// this is where the magic happens, we filter on tag.draft, if it's true, it will get deleted
a_filter = function (tag) {
    return Boolean(tag.draft);
};

a_callback = function (result) {
    console.log (result);
};

githubRemoveAllReleases(AUTH, process.env.GITHUB_OWNER, process.env.GITHUB_REPO, a_callback, a_filter);

Get yourself a token and create a .env file like this:

GITHUB_OWNER=owner
GITHUB_REPO=repo
GITHUB_TOKEN=your_token

Then run this (a few times as it does not support pagination):

npm main.js
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
  • 1
    Thanks. It works. People need to be aware that the only output you get is "null" without telling you it did delete something. – Nordes Mar 28 '17 at 19:29
  • 1
    Good point, I suppose it's possible to fork `github-remove-all-releases`, edit its `index.js` by adding some `console.log` or even adding some more code in my snippet in the `a_filter ` function, something like `if (Boolean(tag.draft)) { console.log(tag + "will be deleted"); return true; } else { return false; }` (untested, but you get the idea). ;) – GabLeRoux Mar 28 '17 at 19:40
  • I wish people down-voting could write why they did so. I'm wondering what went wrong as this totally worked on my side. Plus, `hub`'s [pull request is now merged](https://github.com/github/hub/pull/1327) so one can simply execute `hub release delete `. Updating answer accordingly :) – GabLeRoux Feb 27 '18 at 16:07