Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Archive all of the messages in your Facebook Messages Inbox

Facebook - Archive All Messages

Because who doesn't have an inbox full of "I got a new phone", event-based group chats, and old lingering messages in their Facebook Messages?

Surprisingly, Facebook has not implemented a way to archive many messages in your inbox. This script provides that solution.

Quickstart

Load Facebook Messages in a new tab.

Open the JavaScript console and paste the contents of jquery.min.js into the console.

Paste the contents of archive-all-facebook-messages.js into the console.

If you want to only test the results before actually running the archiving, there's a param for that:

archive_all(testOnly=true);

When you're ready to run for real:

archive_all();

Caveats

  • Currently only detects the list of messages visible to the user in the page. You can work around this by repeatedly scrolling the message list pane to the bottom until all messages are loaded. I plan to automate this step soon.
  • There is no way to whitelist certain messages from being archived. Currently, you can manually unarchive select messages from the archived view.
  • Sometimes the messages page remains cached after the script reloads it. Reloading it once by hand solves this.
function archive_all(testOnly) {
var someMessages, archiveButton;
if (testOnly === "undefined") { testOnly = false; }
someMessages = $("li._k- span.accessible_elem");
console.log("Found", someMessages.length, "messages to archive in your inbox.");
archiveButton = null;
someMessages.each(function () {
if (testOnly) {
console.log("*DEBUG Only* Archived:", $(this).text());
} else {
archiveButton = $(this).parent().parent().parent().prev().children()[1];
archiveButton.click();
}
});
if (!testOnly) {
location.reload();
}
}
@luvittor

This comment has been minimized.

Copy link

commented Jan 21, 2016

I made some modifications so I could expand to older messages without archiving and I could delete messages from and i > 0.

function archiveAll(isTest) {
    var click = function(elem){
        (window.dispatchEvent) ? elem.dispatchEvent(new MouseEvent('click')) : elem.fireEvent("onclick");
    }

    var messages = document.querySelectorAll("li._k- span.accessible_elem");
    console.log("Found %s messages to archive in your inbox.", messages.length);

    archived = 0;

    for(var i = 300, numMessages = messages.length; i < numMessages; ++i){
        var message = messages[i];
        var archiveButton = message.parentNode.parentNode.parentNode.parentNode.firstChild.childNodes[1];

        console.log("message", i);
        console.log("message", message.firstChild);
        console.log("message", (!isTest) ? "archive" : "test archive");
        if (!isTest) click(archiveButton);

        archived++;
    }

    console.log("message", "total archived");
    console.log("message", archived);

    // if (!isTest) { location.reload(); }
}

function loadOlderMsgs() {
    var click = function(elem){
        (window.dispatchEvent) ? elem.dispatchEvent(new MouseEvent('click')) : elem.fireEvent("onclick");
    }

    var loadOlder = document.getElementsByClassName("uiMorePagerPrimary")[0];
    var loadOlderStr = loadOlder.innerHTML;
    if( -1 < loadOlderStr.indexOf("Load Older Threads") ){ click(loadOlder); }

    var messages = document.querySelectorAll("li._k- span.accessible_elem");
    numMessages = messages.length;

    console.log("message", "total displayed");
    console.log("message", numMessages);
}

loadOlderMsgs();
archiveAll();
@scor314on

This comment has been minimized.

Copy link

commented Jan 13, 2017

Hey there, Facebook newly changed the messages link, they integrated system with the messenger. So these javascripts are not working anymore. Could you write a new javascript for archiving all messages.

@Ch3rrouT

This comment has been minimized.

Copy link

commented Jan 28, 2017

Yeah i have the same problem

@elibosley

This comment has been minimized.

Copy link

commented Nov 1, 2017

Hey, I've updated this to work on the newest version of messenger. See here: HERE

@darkdoctor

This comment has been minimized.

Copy link

commented Jan 3, 2019

Not working, i get 0 messages found,

@EmmanuelOga

This comment has been minimized.

Copy link

commented Mar 5, 2019

(function run() {
  let all = document.querySelectorAll('div[aria-label="Conversation actions"]');
  if (all.length == 0) return;
  let a = all[0];
  a.click();
  setTimeout(() => {
    document.querySelectorAll('a[role=menuitem]').forEach(act => {
      if (act.innerText.match(/Archive/)) act.click();
    });
    run();
  }, 250);
})();
@Hkattelu

This comment has been minimized.

Copy link

commented May 27, 2019

This is now a Chrome extension.

It operates similar to Emmanuel's comment above. See this repo for the implementation for the more recent versions of messenger.

@trubesv

This comment has been minimized.

Copy link

commented Jun 25, 2019

Hello,

I can confirm @EmmanuelOga code is working perfectly, and is runnable directly from messenger.com.
Other tools and extensions did not work for me.

If you want to archive everything at the click of a button, simply create a bookmark with the code as URL, prefixed by javascript:.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.