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();
@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];

@shev72
Copy link

shev72 commented Dec 4, 2024

this worked today:

(async function unfollowAutomation() {
console.log("Starting to unfollow users...");

// Scroll to load all "Following" items
async function scrollToLoad() {
    let scrollBox = document.querySelector('div[role="dialog"] ul'); // Target the scrolling box
    let lastScrollHeight = 0;

    while (scrollBox.scrollHeight !== lastScrollHeight) {
        lastScrollHeight = scrollBox.scrollHeight;
        scrollBox.scrollTo(0, lastScrollHeight);
        await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second to allow content to load
    }
    console.log("Finished scrolling through the list.");
}

// Unfollow function
async function unfollowUsers() {
    const unfollowButtons = Array.from(document.querySelectorAll('button')).filter(
        button => button.innerText === "Following"
    );

    for (let i = 0; i < unfollowButtons.length; i++) {
        unfollowButtons[i].click(); // Click the "Following" button
        await new Promise(resolve => setTimeout(resolve, 500)); // Wait for half a second

        const confirmButton = document.querySelector('button:contains("Unfollow")'); // Look for the confirmation button
        if (confirmButton) {
            confirmButton.click();
        }

        console.log(`Unfollowed ${i + 1}/${unfollowButtons.length}`);
        await new Promise(resolve => setTimeout(resolve, 2000)); // Wait 2 seconds between actions to avoid being flagged
    }

    console.log("Unfollowing completed.");
}

// Run the script
await scrollToLoad();
await unfollowUsers();

})();

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