Skip to content

Instantly share code, notes, and snippets.

@mdunham
Last active May 1, 2022 03:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mdunham/aea1668dbd1c70b51ea44f99e5bca9b6 to your computer and use it in GitHub Desktop.
Save mdunham/aea1668dbd1c70b51ea44f99e5bca9b6 to your computer and use it in GitHub Desktop.
How to delete all Facebook conversations easily. The script to be run inside Chrome's JavaScript console to delete all the messages and conversations from your Facebook inbox. Instruction: 1 - open your Facebook inbox folder https://www.facebook.com/messages/ 2 - open the Chrome JS console (ALT+CMD+J on Mac OS X) 3 - paste the script and press e…
/**
* Auto Delete All Facebook Messages
*
* Usage:
* 1. Navigate to your messages page https://www.facebook.com/messages
* 2. Open the console in dev tools
* 3. Paste all of the code below, and hit enter
*
* ---> Does not work on mobile chrome <---
*
* @author Matthew Dunham <matt@matthewdunham.net>
*/
(function(){
var
// Recursive Method
_reInit = function(count){
// Holds the business logic
var _init = function(reInit){
var
// Clicks the delete convo button that activate the confirmation
_startDelete = function(){
var
nameFrom = document.querySelector('#webMessengerHeaderName').innerText,
_findDelete = function(elements){
for (var i = 0; i < elements.length; i++) {
var ele = elements[i];
if (-1 !== (ele.innerHTML).indexOf('Delete Conversation')) {
console.log('Deleting Messages From: ' + nameFrom);
ele.click();
return true;
break;
}
}
return false;
};
if ('No Selection' === nameFrom) {
_nextConvo();
return;
}
if (true === _findDelete(document.querySelectorAll('a[role="menuitem"]'))){
setTimeout(_confirmDelete, 500);
} else {
var actLink = document.querySelector('button[data-tooltip-content="Actions"]');
if (actLink) {
actLink.click();
setTimeout(reInit, 2000);
} else {
console.log('Unable to locate the delete button');
return;
}
}
},
// Triggers more messages to load
_scrollLoad = function() {
var _scrollRegion = document.querySelector('div.uiScrollableAreaWrap.scrollable');
if ('undefined' === typeof _scrollRegion || false === _scrollRegion.hasOwnProperty('scrollHeight')) {
console.log('Unable to locate the messages scroll region');
return false;
}
_scrollRegion.scrollTop = _scrollRegion.scrollHeight;
},
// Clicks the button in the dialog box
_confirmDelete = function(found){
var eles = document.querySelectorAll('div[role="dialog"] a[action="cancel"]');
if ('undefined' === typeof found) {
found = false;
}
for (var i = 0; i < eles.length; i++) {
var ele = eles[i];
if (-1 !== (ele.innerHTML).indexOf('Delete Conversation')) {
console.log('Deleting Messages - Confirming Now');
found = true;
ele.click();
break;
}
}
if (true === found){
_nextConvo();
} else {
console.log('Unable to locate the confirm delete button');
}
},
// Selects the next conversation to delete
_nextConvo = function(){
setTimeout(function(){
var nextConvo = document.querySelectorAll('._k_');
if ( ! nextConvo.length) {
if (false === _scrollLoad()) {
// Probably done at this point, or in error
console.log('Unable to locate any messages');
return true;
}
}
nextConvo[0].click();
setTimeout(reInit, 1000);
}, 250);
};
// Starts the process - step 1
_startDelete();
};
_init(_reInit);
};
_reInit(0);
})();
Simple Method
An easier way to run the script is by simply creating a bookmark in your browser using the following:
Completely, safe.
Bookmark Name: Delete All FB Conversation
Bookmark Url : javascript:!function(){var e=function(o){var n=function(e){var o=function(){var o=document.querySelector("#webMessengerHeaderName").innerText,n=function(e){for(var n=0;n<e.length;n++){var t=e[n];if(-1!==t.innerHTML.indexOf("Delete Conversation"))return console.log("Deleting Messages From: "+o),t.click(),!0}return!1};if("No Selection"===o)return void l();if(!0===n(document.querySelectorAll('a[role="menuitem"]')))setTimeout(t,500);else{var r=document.querySelector('button[data-tooltip-content="Actions"]');if(!r)return void console.log("Unable to locate the delete button");r.click(),setTimeout(e,2e3)}},n=function(){var e=document.querySelector("div.uiScrollableAreaWrap.scrollable");return"undefined"==typeof e||!1===e.hasOwnProperty("scrollHeight")?(console.log("Unable to locate the messages scroll region"),!1):void(e.scrollTop=e.scrollHeight)},t=function(e){var o=document.querySelectorAll('div[role="dialog"] a[action="cancel"]');"undefined"==typeof e&&(e=!1);for(var n=0;n<o.length;n++){var t=o[n];if(-1!==t.innerHTML.indexOf("Delete Conversation")){console.log("Deleting Messages - Confirming Now"),e=!0,t.click();break}}!0===e?l():console.log("Unable to locate the confirm delete button")},l=function(){setTimeout(function(){var o=document.querySelectorAll("._k_");return o.length||!1!==n()?(o[0].click(),void setTimeout(e,1e3)):(console.log("Unable to locate any messages"),!0)},250)};o()};n(e)};e(0)}();
Then navigate to https://www.facebook.com/messages and simply click on the bookmark you just created.
@mdunham
Copy link
Author

mdunham commented Aug 8, 2016

I wrote this in just under 30 minutes off the top of my head, but it works well :-)

@pudasainianjan
Copy link

does this unsend from both sides?

@KevinMoolana
Copy link

I am trying to apply that to the archived messages. Go to https://www.facebook.com/messages then from the droplist >> choose Archived... but it does not work on Chrome.. Any modification can be done to make it target/delete the archived messages?

@techsamy
Copy link

techsamy commented Jan 8, 2022

Not working, Please update the code

@GeekHamza1
Copy link

update your shit

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