How to unfollow all on GitHub

terminal cli code code archive github network_node graphql terminal powershell

Some time ago I was experimenting with GraphQL and I wrote some code to follow a lot of users on Github. Specifically, I followed everyone who was a contributor on my favorite repositories. While I don’t have that code anymore, I have now written some code to do the reverse. That is, I have written code to unfollow everyone I am following.

I’ve created a gist for this so you can try it on your own.

I am using powershell for the main script, and the GitHub CLI to execute my GitHub GraphQL queries and mutations.

Before geting started, make sure you have installed the GitHub CLI. After that, you’ll want to authenticate yourself using the command gh auth login with a personal access token that has the user:follow scope.

following.graphql

This query retrieves the ids of the users we are following, in batches of a 100.

The pageinfo, endCursor stuff is necessary for pagination to work - later, in the powershell script, we’ll use the --paginate option of the CLI to retrieve all the ids of users we are following and not just the 100 which is the maximum allowed in a single batch.

unfollow.graphql

The above is a mutation that unfollows a single user. We’ll execute this mutation once for each user we are following.

unfollow_all.ps1

This powershell script calls the (gh) CLI once to retrieve the ids of all the users we are following. The result is not well-formated json, so we first have to fix that - that’s what the $str is for. Then, we call the CLI once for each id to unfollow the respective user.

As for the powershell stuff, I’m only using a couple of commandlets:

I’m also using a regular expression (\}\s*\{\s*"data") to insert a comma between the pages that are returned by the following.graphql query.

https://michalis.site/2020/10/12/unfollow-all.html