How to delete old Facebook posts from the browser console

You're here because you want to delete posts form Facebook? Good, keep reading.

Concept

I wanted to be able to delete specific years of posts. I wanted to delete my old posts keeping the new alive. As of this writing Facebook does not let you delete posts in bulk, the UI is garbage and full of modals. I guess in an attempt to dissuade you from deleting the data.

You can delete all the data but not targeted years.

How to see your old posts?

The current URL if the following, again it might've changed by the time you read this.

If you access through the menus try something like Settings > Privacy > Timeline > Posts

https://www.facebook.com/YOUR_USERNAME/allactivity?entry_point=privacy_settings&privacy_source=activity_log&log_filter=cluster_11&category_key=statuscluster

JavaScript

The concept is quite simple. Mimic what you would do with your mouse with JavaScript. The core functionality of the script is given by setInterval and setTimeout, then we simply perform click() on certain elements.

The script relies 100% on the markup, Facebook might change that. Use at your own risk, the code might not work. I don't think it might be dangerous but could simply not find the DOM elements needed to perform the actions.

Have a look and enjoy!

Last thoughts

If you're interested in privacy and sharing your point of view, read my thoughts on my other blog.

/**
 * HOW TO
 * Replace XXXXXX by your username in the following url
 * https://www.facebook.com/XXXXXX/allactivity?entry_point=privacy_settings&privacy_source=activity_log&log_filter=cluster_11&category_key=statuscluster
 * Scroll down to a year.
 * Important: you must scroll and see all the posts you want to delete before executing.
 * The script will not auto scroll, will not auto load the posts.
 *
 * Set the year in the configuration.
 * Copy the whole thing.
 * Paste in the Dev Tools console.
 *
 * NOTICE: the code relies on Facebook's markup. The classnames or structure could chance any time.
 * The code might not work but it is not dangerous either. Run safely.
 */

/**
 * Configuration
 */
const year = '2013';

/**
 * Magic
 */
const SELECTORS = {
  container: `#year_${year}`,
  editButton: `a[aria-label="Edit"]`,
  openLayer: `.uiContextualLayerPositioner.uiLayer`,
  deleteButton: `[rel="async-post"][role="menuitem"]`,
  confirmButton: `button.layerConfirm`
}
const CLASSES = {
  hidden: `hidden_elem`
}

setInterval (() => {
  // Find the year container
  const $container = document.querySelector(SELECTORS.container);
  const $editButtons = $container.querySelectorAll(SELECTORS.editButton);

  if ($editButtons.length > 0) {
    const $button = $editButtons[0];

    // Open the edit with a delay
    $button.click();

    const deleteTimeout = window.setTimeout(() => {
      // Find the actual open layer.
      // Once you delete an element the layer stays on the page
      const $openLayers = document.querySelectorAll(SELECTORS.openLayer);

      // Loop until you find one without the hidden class
      $openLayers.forEach($layer => {
        if (!$layer.classList.contains(CLASSES.hidden)) {
          // This is the one, the one visible
          const $deleteButton = $layer.querySelector(SELECTORS.deleteButton);

          if ($deleteButton) {
            $deleteButton.click();

            const confirmTimeout = window.setTimeout(() => {
              const $confirmButton = document.querySelector(SELECTORS.confirmButton);

              if ($confirmButton) {
                $confirmButton.click();
              }
            }, 3000);
          }
        }
      });
    }, 1500);
  }
}, 13000);Code language: PHP (php)

Leave a Reply

Your email address will not be published. Required fields are marked *