Categories
Tutorials

How to delete all posts from Facebook page

Table of Contents

I created a script that you can run in your console on Facebook that will help you delete all your Facebook posts in a page.

I had an old page that I wanted to repurpose. Its links linked to a website that is no longer there. But to delete posts one by one … uh … I’d rather write code that did that for me.

The reason why I created this is because all the options I found online didn’t work.

They required you to use Publishing Tools, which has been deprecated (in non-programmer English, that means it’s obsolete and no longer there).

There’s also another suggestion about going to Creator Studio to delete all posts. Here’s a website with a good guide. Try this first.

But for some reason, I can’t see ALL the posts there, so I couldn’t delete it that way.

I’d suggest you try the above methods before you try the script. The script works but it’s not something for beginners to use.

How to use:

  1. Open the Facebook page in which you want to delete ALL posts
  2. Go to your browser’s console
  3. Paste in this code and run it.
  4. Leave it to run

How to improve it (aka shortcomings):

  • This code will crash if you have something that does not have a “Delete post” options. For example, a cover photo post.
    — In this case, just hide that post, refresh and re-run the script.
    — if someone would like to write code that will skip the element, please go ahead and fork my code.
    You can probably write a function that goes through document.querySelectorAll("div[aria-label='Actions for this post']") and goes to the next item on the NodeList if [0] doesn’t have a “Delete post” option.
    Alternatively, just write an if-else statement that hides that post and reruns the script.
  • Code uses timers instead of callbacks. Every element needs to be loaded, so I put timers instead of callbacks. The justification is that it’s easier this way even though the code is definitely not my proudest moment.

Code

let menuButton;

setInterval(() => {
  menuButton = document.querySelector(
    "div[aria-label='Actions for this post']"
  );
  menuButton.click();

  setTimeout(() => {
    let deletePost = document.evaluate(
      "//span[text()='Delete post']",
      document,
      null,
      XPathResult.ANY_TYPE,
      null
    );
    let deletePostElement = deletePost.iterateNext();
    setTimeout(() => {
      deletePostElement.click();
      setTimeout(() => {
        let deleteModal = document.evaluate(
          "//span[text()='Delete']",
          document,
          null,
          XPathResult.ANY_TYPE,
          null
        );
        console.log(deleteModal);
        let deleteModalElement = deleteModal.iterateNext();
        console.log(deleteModalElement);
        setTimeout(() => {
          deleteModalElement.click();
          3;
        }, 5000);
      }, 6000);
    }, 5000);
  }, 3000); //lets the menu load
}, 25000);

Have a project in mind?

Websites. Graphics. SEO-oriented content.

I can get your next project off the ground.

See how I have helped my clients.