Skip to content

Instantly share code, notes, and snippets.

@tedmiston
Last active March 31, 2026 06:07
Show Gist options
  • Select an option

  • Save tedmiston/c7ac401da96b55022aaf to your computer and use it in GitHub Desktop.

Select an option

Save tedmiston/c7ac401da96b55022aaf to your computer and use it in GitHub Desktop.
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();
}
}
@abaddonpro
Copy link
Copy Markdown

@elitetrollz thank you so much, worked for me as of today!

@DividedGibon
Copy link
Copy Markdown

DividedGibon commented Jan 16, 2023

Hello - this is my first Github post, so go easy on me :)

I found the code mentioned above was quite slow to run, particularly with Marketplace chats - but did work. It's worth noting I had an exceptionally high number of chats (minimum 2000 plus).

Obviously this is down to the set timeout, the iteration of the function, lack of caching etc. I suggest using the following code which uses requestAnimationFrame, caches 'div[role=menuitem]' etc. There is further improvements that could be made here but this worked a lot quicker for me; not lightning speed but instantly reduces the iteration time from 500ms to however long it takes the browser to be ready to repaint the screen.

(function run() { let all = document.querySelectorAll('div[aria-label="Menu"]'); if (all.length == 0) return; let a = all[1]; a.click(); let menuitems = document.querySelectorAll('div[role=menuitem]'); let archiveChatRegex = /Archive chat/; for (let i = 0; i < menuitems.length; i++) { if (archiveChatRegex.test(menuitems[i].innerText)) { menuitems[i].click(); } } requestAnimationFrame(run); })();

VERSION with white space removed: (function run(){let all=document.querySelectorAll('div[aria-label="Menu"]');if(all.length==0)return;let a=all[1];a.click();let menuitems=document.querySelectorAll('div[role=menuitem]');let archiveChatRegex=/Archive chat/;for(let i=0;i<menuitems.length;i++){if(archiveChatRegex.test(menuitems[i].innerText)){menuitems[i].click();}}requestAnimationFrame(run);})();

Notes
I did not need to paste the http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js contents.
The Dev Console will throw a "[Violation] 'requestAnimationFrame' handler took ms" error for each chat but this can be ignored

@klydeinside
Copy link
Copy Markdown

klydeinside commented Feb 1, 2023

Thank you so much! I just had 100 Marketplace conversations and this saved me so much time. Also, for easier usage, just drag the code to the bookmarks and then just click it when you need it. It looks like you would need to add javasript tag to the front before it'll work. At least for me on Brave. It would look like so:

javascript: (() => {
(function run(){let all=document.querySelectorAll('div[aria-label="Menu"]');if(all.length==0)return;let a=all[1];a.click();let menuitems=document.querySelectorAll('div[role=menuitem]');let archiveChatRegex=/Archive chat/;for(let i=0;i<menuitems.length;i++){if(archiveChatRegex.test(menuitems[i].innerText)){menuitems[i].click();}}requestAnimationFrame(run);})();
})();

Drag all of that code to your bookmark toolbar and it'll put it there. Then, you can use it whenever you like.

@robovo
Copy link
Copy Markdown

robovo commented Mar 23, 2023

Thank you all! This has done a heap of tedious cleanup work for me. There is just something so satisfying in seeing things happen on their own instead of your repeating clicking. This is just perfect.

@lamyergeier
Copy link
Copy Markdown

Would be nice if we could also remove the marketplace messages.

@stacksjb
Copy link
Copy Markdown

THANK YOU! I've tried several solutions across the internet and this is the first that worked both fast and successfully.

However, I've noticed it doesn't work on archived messages. Any suggestions for the archived chat page?

@DividedGibon
Copy link
Copy Markdown

Would be nice if we could also remove the marketplace messages.

Did you attempt the code I pasted above? It should also work for market place messages.

@DividedGibon
Copy link
Copy Markdown

THANK YOU! I've tried several solutions across the internet and this is the first that worked both fast and successfully.

However, I've noticed it doesn't work on archived messages. Any suggestions for the archived chat page?

If it was my code you used (a few comments up) then it won’t work on archived chats as it’s archiving them. Would you want to delete?

@stacksjb
Copy link
Copy Markdown

stacksjb commented Apr 18, 2023

Hello - this is my first Github post, so go easy on me :)

I found the code mentioned above was quite slow to run, particularly with Marketplace chats - but did work. It's worth noting I had an exceptionally high number of chats (minimum 2000 plus).

Obviously this is down to the set timeout, the iteration of the function, lack of caching etc. I suggest using the following code which uses requestAnimationFrame, caches 'div[role=menuitem]' etc. There is further improvements that could be made here but this worked a lot quicker for me; not lightning speed but instantly reduces the iteration time from 500ms to however long it takes the browser to be ready to repaint the screen.

(function run() { let all = document.querySelectorAll('div[aria-label="Menu"]'); if (all.length == 0) return; let a = all[1]; a.click(); let menuitems = document.querySelectorAll('div[role=menuitem]'); let archiveChatRegex = /Archive chat/; for (let i = 0; i < menuitems.length; i++) { if (archiveChatRegex.test(menuitems[i].innerText)) { menuitems[i].click(); } } requestAnimationFrame(run); })();

VERSION with white space removed: (function run(){let all=document.querySelectorAll('div[aria-label="Menu"]');if(all.length==0)return;let a=all[1];a.click();let menuitems=document.querySelectorAll('div[role=menuitem]');let archiveChatRegex=/Archive chat/;for(let i=0;i<menuitems.length;i++){if(archiveChatRegex.test(menuitems[i].innerText)){menuitems[i].click();}}requestAnimationFrame(run);})();

Notes I did not need to paste the http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js contents. The Dev Console will throw a "[Violation] 'requestAnimationFrame' handler took ms" error for each chat but this can be ignored

I used this version - VERY FAST.

@DividedGibon
Copy link
Copy Markdown

Hello - this is my first Github post, so go easy on me :)
I found the code mentioned above was quite slow to run, particularly with Marketplace chats - but did work. It's worth noting I had an exceptionally high number of chats (minimum 2000 plus).
Obviously this is down to the set timeout, the iteration of the function, lack of caching etc. I suggest using the following code which uses requestAnimationFrame, caches 'div[role=menuitem]' etc. There is further improvements that could be made here but this worked a lot quicker for me; not lightning speed but instantly reduces the iteration time from 500ms to however long it takes the browser to be ready to repaint the screen.
(function run() { let all = document.querySelectorAll('div[aria-label="Menu"]'); if (all.length == 0) return; let a = all[1]; a.click(); let menuitems = document.querySelectorAll('div[role=menuitem]'); let archiveChatRegex = /Archive chat/; for (let i = 0; i < menuitems.length; i++) { if (archiveChatRegex.test(menuitems[i].innerText)) { menuitems[i].click(); } } requestAnimationFrame(run); })();
VERSION with white space removed: (function run(){let all=document.querySelectorAll('div[aria-label="Menu"]');if(all.length==0)return;let a=all[1];a.click();let menuitems=document.querySelectorAll('div[role=menuitem]');let archiveChatRegex=/Archive chat/;for(let i=0;i<menuitems.length;i++){if(archiveChatRegex.test(menuitems[i].innerText)){menuitems[i].click();}}requestAnimationFrame(run);})();
Notes I did not need to paste the http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js contents. The Dev Console will throw a "[Violation] 'requestAnimationFrame' handler took ms" error for each chat but this can be ignored

I used this version - VERY FAST.

Hi, yes this is mine. This code is archiving the chats it sees, therefore it won’t work on already archived chats. Leave it with me and I’ll re write to delete archived chats.

@xBenji65
Copy link
Copy Markdown

xBenji65 commented May 7, 2023

Any chance the opposite could be implemented? A way to unarchive all the chats.

@DividedGibon
Copy link
Copy Markdown

Hi xBenji65. I'm actually just going to set up a separate repo to store the updated code. I'll include a version which does this too. Give me an hour or so.

@DividedGibon
Copy link
Copy Markdown

Any chance the opposite could be implemented? A way to unarchive all the chats.

Check out here : https://github.com/DividedGibon/Facebook_Messages_Bulk

@DividedGibon
Copy link
Copy Markdown

THANK YOU! I've tried several solutions across the internet and this is the first that worked both fast and successfully.
However, I've noticed it doesn't work on archived messages. Any suggestions for the archived chat page?

If it was my code you used (a few comments up) then it won’t work on archived chats as it’s archiving them. Would you want to delete?

Check out here : https://github.com/DividedGibon/Facebook_Messages_Bulk

@DividedGibon
Copy link
Copy Markdown

Would be nice if we could also remove the marketplace messages.

Check out here : https://github.com/DividedGibon/Facebook_Messages_Bulk

@athachai29
Copy link
Copy Markdown

Thanks a lot

@capoexe
Copy link
Copy Markdown

capoexe commented Mar 31, 2026

had to make a new account for this, anyways this doesnt work anymore and what seemed to work is this, make sure you're in the "messenger.com" website and press f12 and go to the console and paste this and enter

edit: in order to stop it or if its done just type "clearInterval(the number it showed after u entered)"

setInterval(() => { let buttons = document.querySelectorAll('[role="button"]'); for (let btn of buttons) { let label = btn.getAttribute('aria-label'); if (label && label.startsWith('More options for')) { btn.click(); setTimeout(() => { document.querySelectorAll('div[role=menuitem]').forEach(item => { if (item.innerText.includes('Archive chat')) item.click(); }); }, 800); break; } } }, 1500);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment