Automated Facebook unfollowing and following

Published April 10, 2023

As social media continues to be an integral part of our lives, it’s important to manage our online presence and experiences. For me, Facebook was becoming overwhelming with the clutter and negativity on my timeline. I found myself constantly scrolling past posts that didn’t interest me or that were simply too negative.

That’s when I decided to take control of my Facebook experience by creating scripts to automate the process of following and unfollowing people. By doing so, I could curate my timeline to only show the content that I wanted to see.

Using these scripts, I was able to follow people who shared content that aligned with my interests and unfollow those who consistently posted negative or irrelevant content. This allowed me to have a more positive and personalized experience on Facebook.

How it works

  1. Ensure that your Facebook interface language is set to English. This is important because the scripts use the English text to identify the elements on the page.
  2. Go to https://www.facebook.com/friends/list
  3. In the list on the left, make sure you scroll to the bottom to load the full list of friends.
  4. Open your browser devtools https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Tools_and_setup/What_are_browser_developer_tools#how_to_open_the_devtools_in_your_browser
  5. Paste the relevant code in the console of the devtools.
  6. Wait until you see no more menus pop up automatically.

Unfollow everyone

(async () => {
    sleep = (ms) => new Promise((r) => setTimeout(r, ms));

    mores = Array.from(
        document.querySelectorAll(
            '[aria-label="All friends"] [aria-label="More"]'
        )
    );

    for (more of mores) {
        await new Promise(async (resolve, reject) => {
            more.click();

            await sleep(10);

            more.scrollIntoView();

            const unfollow = Array.from(
                document.querySelectorAll('[role="menuitem"]')
            )?.find((el) =>
                el.textContent.contains('Stop seeing posts but stay friends.')
            );

            if (!unfollow) {
                more.click();

                return resolve();
            }

            unfollow.click();

            await sleep(2000);

            return resolve();
        });
    }
})();

Refollow everyone

(async () => {
    sleep = (ms) => new Promise((r) => setTimeout(r, ms));

    mores = Array.from(
        document.querySelectorAll(
            '[aria-label="All friends"] [aria-label="More"]'
        )
    );

    for (more of mores) {
        await new Promise(async (resolve, reject) => {
            more.click();

            await sleep(10);

            more.scrollIntoView();

            const follow = Array.from(
                document.querySelectorAll('[role="menuitem"]')
            )?.find((el) => el.textContent.contains('See posts from'));

            if (!follow) {
                more.click();

                return resolve();
            }

            follow.click();

            await sleep(2000);

            return resolve();
        });
    }
})();

The code is also posted in a gist .