4 minute read

Delete All Your Old Facebook Posts with a Few Lines of Javascript

Oct 17, 2018 / Javascript

Last Updated: Tuesday, November 13th, 2018

Intro

The other day I was scrolling through Facebook when I noticed a feature I hadn’t seen before — Activity Log. This feature gives you a zoomed-out look of your timeline that allows you to jump through to different years and see the posts, comments, and pictures from today back to the beginning.

After a few minutes skimming through old posts from my days as a 14 year old internet spazz, I realized I had thousands of posts that needed to be deleted. To click and manually delete each post could have taken weeks, so instead I wrote a few lines of Javascript that would traverse the DOM for “delete” button nodes, click them, and instantly delete my entire post history in a matter of seconds.

This post is still evolving, right now I’m going to admit the code is pretty manual. If it catches enough traction I’ll package it all up in a Chrome extension and make a link available here.

Also, if you’re not very technical and don’t want to bother with the code in this post, there are a couple extensions on the market that can give you the ability without code: https://chrome.google.com/webstore/detail/delete-all-facebook-posts/

Navigate to Your Post History in Facebook

To prepare your browser, you’ll want to navigate to your Activity Log. See below:

First, open your Facebook settings:

Then, navigate to “Your Facebook Information”:

Click “Access Your Information”:

Access “Your Posts”:

Now, you’re hopefully looking at a list of your Facebook posts grouped by year. You should see a ‘card’ UI element that holds each post, with a few buttons on the top right of each card that look something like:

The Code

At this time, you’ll want to go ahead and open your browser’s developer tools > Javascript console tab. Facebook has a couple warning messages about entering malicious code given to you by strangers on the internet, but we’ll just ignore that for now  ¯\_(ツ)_/¯

The first thing that we’ll want to do is gain access to the list of DOM nodes that house the edit button:

The button to delete a post hides within the dropdown that is triggered by clicking the edit icon:

First, you’ll want to scroll as far down as possible to the very beginning of your Facebook timeline history. You can do this manually, or automate by typing this code into your Javascript console:

// First, enter this:
var scrollToBottom = function() {
  window.scrollTo(0, document.body.scrollHeight);
}

// Then, call:
scrollToBottom(); // as many times as it takes you to get to the bottom of the page.

We can grab a list of DOM Nodes by entering the following code into our Javascript console:

const editButtons = document.querySelectorAll("[data-tooltip-content='Edit']");

To speak a little bit about the querySelector we used, we grabbed the edit buttons with the HTML attribute data-tooltip-content. I tried grabbing the edit buttons by class or ID, but Facebook’s front end framework makes that very difficult with very cryptic class and id names.

Once you press enter, you’ll now have the list of all edit button nodes stored in a variable called editButtons.

Now, we’ll want to open each of the dropdowns by simulating a click event on each one of the edit buttons:

for (i = 0; i < editButtons.length; i++) {
  editButtons[i].click();
}

You may not be able to see the actual dropdown, but you just made the DOM’s delete button nodes available for you to store in another variable:

const deleteButtons = document.querySelectorAll("[ajaxify*='/ajax/timeline/delete']");

Now that you have the delete button node list stored in your deleteButtons variable, you’ll want to simulate a click event on each one of those as well:

for (i = 0; i < deleteButtons.length; i++) { deleteButtons[i].click(); setTimeout(() => { console.log('deleting') }, 2000)
}

The reason for the timeout is that Facebook renders a confirmation modal that asks you if you are sure that you want to delete the current post in the loop. You can either write another script to automate click events on each one of those delete buttons, or you can just click them manually.

This script can easily be turned into a Chrome/Firefox extension, which I am currently working on! I’ll be updating that post as I make progress. For now, please enjoy your brand new, guilt-free Facebook timeline!