Skip to content

Instantly share code, notes, and snippets.

@mikeknapp
Created January 18, 2023 04:48
Show Gist options
  • Save mikeknapp/788cb26da096161ef3fff1015d947405 to your computer and use it in GitHub Desktop.
Save mikeknapp/788cb26da096161ef3fff1015d947405 to your computer and use it in GitHub Desktop.
Remove certain instagram followers in bulk
/*
Adapted from https://gist.github.com/adminy/5e80c40592b135e6d7fd8e6bd88c825a
Use something like https://gist.github.com/abir-taheer/0d3f1313def5eec6b78399c0fb69e4b1#file-instagram-follower-following-js to get the list of
users to unfollow.
Instructions:
1. Open: https://www.instagram.com/{YOUR_USERNAME}/following/ in your web browser <-- replace
{YOUR_USERNAME} with your username
2. Edit the `usersToUnfollow` array below to contain the usernames of the people you want
to unfollow.
3. Open the dev console and paste the code below.
4. The code will stop once you've unfollowed 140 people, or if there is no one else to
unfollow. If you want to unfollow more people, repeat steps 1-3 at least 24 hours later so that you
don't get your account banned.
Use at your own risk! I'm not responsible for any bans that may occur.
*/
var usersToUnfollow = [
// TODO: Fill in the usernames of the people you want to unfollow.
"username1",
"username2",
];
const randInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min); // min and max
async function sleepRand() {
const ms = randInt(2000, 4000);
console.log("sleeping for ", ms);
await new Promise((resolve) => setTimeout(resolve, ms));
}
async function sleepOneMin() {
console.log("sleeping for 1 min");
await new Promise((resolve) => setTimeout(resolve, 60000));
}
let numUnfollows = 0;
async function processFollowerDiv(followerDiv) {
let username = "";
followerDiv.querySelectorAll("a").forEach((a) => {
if (a.hasAttribute("href")) {
temp = a.href.split("/").slice(-2)[0];
if (temp != "") {
username = temp;
}
}
});
if (usersToUnfollow.indexOf(username) > -1) {
console.log("UNFOLLOWING: ", username);
const followingButton = followerDiv.querySelectorAll("button")[0];
followingButton.click();
await sleepRand();
// Confirm that we wan't to unfollow.
const confirmDialog = document.querySelectorAll('[role="dialog"]')[2];
const unfollowConfirm = confirmDialog.querySelectorAll("button")[0];
unfollowConfirm.click();
numUnfollows++;
usersToUnfollow = usersToUnfollow.filter((item) => item !== username);
await sleepOneMin();
}
}
function findItemContainer(parentNode) {
// Recurse through the dialog node find the node one with at least 10 elements.
const children = parentNode.children;
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (child.children.length >= 10) {
return child;
}
const result = findItemContainer(child);
if (result) {
return result;
}
}
}
async function main() {
const dialogNode = document.querySelectorAll('[role="dialog"]')[0];
const itemContainer = findItemContainer(dialogNode);
while (usersToUnfollow.length > 0) {
console.log("usersToUnfollow", usersToUnfollow.length);
const folllowerNodes = itemContainer.children;
for (let followerDiv of folllowerNodes) {
await processFollowerDiv(followerDiv);
}
if (numUnfollows > 140) {
alert("You've unfollowed too many people. Please wait 24 hours before running this script again.");
return;
}
await sleepRand();
// Scroll to the end of the page.
folllowerNodes[folllowerNodes.length - 1].scrollIntoView(true);
}
}
await main();
@BryTheGenius
Copy link

Any idea why I'm getting a Syntax Error for identifier main?

@mikeknapp
Copy link
Author

mikeknapp commented Feb 7, 2023

@BryTheGenius:

Any idea why I'm getting a Syntax Error for identifier main?

What browser are you using? I just tested this in Chrome on Mac OS, and it worked for me. It could be you're using an older browser?

Also ensure you're on this page:

https://www.instagram.com/<YOUR_USERNAME>/following/

Of course, substitute <YOUR_USERNAME> for your actual username.

@kanziekan
Copy link

I'm getting this error. What does that mean?
image

@mikeknapp
Copy link
Author

Ah, Insta has probably updated their code and this is now broken. I don't have time to investigate, but if someone is able to find the bug, I'm happy to update the code.

@jmcamposdev
Copy link

Hi I have tried it now and it works perfect be careful not to resize the screen to a mobile view you have to see the PC view of the popup "following". In mobile view it looks full height and full width that happened to me and when I refresh it and paste the script in PC view it works.

I hope it helps.

@Uncle-Chicken
Copy link

Anybody knows an easy way to enhance this in a quick and easy way to only unfollow people who dont follow me back?
Since this requires a list of usernames to unfollow.

@mikeknapp
Copy link
Author

Anybody knows an easy way to enhance this in a quick and easy way to only unfollow people who dont follow me back? Since this requires a list of usernames to unfollow.

@Uncle-Chicken Have a look at https://gist.github.com/abir-taheer/0d3f1313def5eec6b78399c0fb69e4b1#file-instagram-follower-following-js

@Uncle-Chicken
Copy link

@mikeknapp thanks! sorry, i didnt see at first that the script was already mentioned in your code comments.
tried both and works flawless so far. only added a bit longer waiting time for unfollowing. better save than sorry.

thanks for your work guys!

@Uncle-Chicken
Copy link

Does anybody maybe have something similar for tiktok? :)

@MaxCollo
Copy link

MaxCollo commented Dec 8, 2023

Thanks for your work !
It's still working for me, just need to adapt a few things to search in the HTML.

Also, you should unter the total unfollow count in the for loop, otherwise it never ends (I assume it's because the list of users is already loaded in this version of instagram and you were trying to scroll done to load some more) :

const folllowerNodes = itemContainer.children; for (let followerDiv of folllowerNodes) { await processFollowerDiv(followerDiv); } if (numUnfollows > 140) { alert("You've unfollowed too many people. Please wait 24 hours before running this script again."); return; }
becomes
for (let followerDiv of folllowerNodes) { await processFollowerDiv(followerDiv); if (numUnfollows > 140) { alert("You've unfollowed too many people. Please wait 24 hours before running this script again."); return; } }

@MaxCollo
Copy link

MaxCollo commented Dec 8, 2023

The corrections to search for the HTML elements :
In the function processFollowerDiv(followerDiv) :
const confirmDialog = document.querySelectorAll('[role="dialog"]')[2];
becomes
const confirmDialog = document.querySelectorAll('[role="dialog"]')[1];

in the function main() :
const dialogNode = document.querySelectorAll('[role="dialog"]')[0];
becomes
const dialogNode = document.querySelectorAll('[class="_aano"]')[0];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment