Skip to content

Instantly share code, notes, and snippets.

@eggplants
Last active May 23, 2022 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eggplants/569bd6de7023442f275659c1aa9cc3d6 to your computer and use it in GitHub Desktop.
Save eggplants/569bd6de7023442f275659c1aa9cc3d6 to your computer and use it in GitHub Desktop.
Delete GitHub issues with GitHub API (Updated-at: 2022-05-16)

How to delete issues on GitHub with GitHub API

Motivation

https://github.community/t/delete-issues-programmatically/14255

td;tr

  1. Learn GitHub GraphQL API
  2. Prepare PAT with permissions of target issue like repo
  3. Get ids of target issues (recomended: search query)
  4. Execute deleteIssue mutation to delete target issues

My implemention with actions/github-script

Complete code

https://github.com/eggplants/ghitter/blob/master/.github/workflows/delete.yml

Summary

To get target ids with search query:

query {
  search(
    type: ISSUE
    last: 100     # take n ids from hit list
    query: "..."  # example: "author:eggplants repo:eggplants/ghitter"
  ) {
    nodes {
      ... on Issue {
        id      # e.g. "I_kwDOGrVx0s5Cuyyr"
        number  # e.g.: 120
      }
    }
  }
}

It returns:

{
  search: {
    nodes: [
      {id: "I_kwDOGrVx0s5Cuyyr", number: 120},
      {...},
    ]
  }
}

To delete the issues:

mutation($issueId: ID!) {
  deleteIssue(input: {
    clientMutationId: "foobar", # any string, even without this field, will work 
    issueId: $issueId
  }) {
    clientMutationId
  }
}

It returns:

{
  deleteIssue: {
    clientMutationId: "foobar"
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment