Skip to content

Instantly share code, notes, and snippets.

@kiki67100
Last active January 25, 2024 15:08
Show Gist options
  • Save kiki67100/6c68436766ea3a037508970999b7e89e to your computer and use it in GitHub Desktop.
Save kiki67100/6c68436766ea3a037508970999b7e89e to your computer and use it in GitHub Desktop.
Delete batch messages Microsoft Teams
/*
By kiki67100 https://gist.github.com/kiki67100
Tested on Google Chrome
Open Microsoft teams web version, select a conversation
Open Developper tools and copy / paste (Change YOUR_NAME by your)
Work on March 2020 maybe no work on the future...
*/
window.id = [];
YOUR_NAME = "YOUR_NAME";
window.idx=0;
setInterval(function() {
jQuery('#page-content-wrapper').find('div[id^=m]').each(function() {
$('virtual-repeat').scrollTop(0);
setTimeout(function(_this) {
//console.log('timeout...');
//console.log('_this = ',_this);
txt = $(_this).text();
if (txt.indexOf(YOUR_NAME) != -1) {
console.log('not inside');
$(_this).css({
'outline': '1px solid red'
});
_id = $(_this).attr('id');
//console.log('_this id =' + _id)
if (window.id.indexOf(_id) == -1) {
window.id.push(_id.substr(1));
//console.log('add');
} else {
//console.log('!!');
}
}else{
//console.log('already inside');
}
}, 1000, this);
});
}, 2000);
/*
- Extract messages id
On Google Chrome developper tools :
copy(id.join("\n"))
Delete a message, copy the ajax request and export as CURL (bash) (on network tools maybe end by "softDelete")
On notepad ++ on replace regex mode
REPLACE (.*)
Your CURL shell command find the id curl 'https://emea.[...]/121545326566?behavior=softDelete by $1 like this 'https://emea.[...]/$1?behavior=softDelete
lauch on linux terminal all lines
*/
Copy link

ghost commented Nov 9, 2022

Hello @Zegorax
First, congrates to everyone. This code is awesome to stop Microsoft abuse. I know people that lost hours by month to delete message by message on teams.
i think update (27-OCT-2022) is not working. Several times does not scroll, It freezes, its gives errors of HTTP 429 Too Many Requests, it has irregular behavior. It deletes messages that appear latter. Tested on EDGE and Chrome

@alexpeter-pen
Copy link

Improvement in two directions: it scrolls up as it meets the elements, and it skips over those that are not to be deleted faster. It still may stop here and there especially if some of the messages are very big, but either zoom in or nudge with the mouse. Good thing is that if you switch from one chat to another it will seemingly start deleting the messages in other chat too. I had probably thousand of messages and it worked quite well and finished that many messages in less than 20 minutes. Sure you will get too many calls occasionally but I would just refresh the screen, start the script again and it would delete if any message was not deleted yet. Taking into account what this script does and how it does it, I do not think that you can expect deleting more than 1 message per second. And this one with a hiccup here and there is doing just that.

 (function() {
     var JQ = jQuery("iframe").contents();
     var queue = [];
     var last_processed = (new Date()).getTime();
     var loading = true;
     var my_name = null;

     var mouse = function(which, element) {
         var evt = element.ownerDocument.createEvent('MouseEvents');

         evt.initMouseEvent(which, true, true,
             element.ownerDocument.defaultView, 1, 0, 0, 0, 0, false,
             false, false, false, 1, null);

         element.dispatchEvent(evt);
     }

     var repeat = setInterval(function() {

         if (loading) {

             if (JQ.find("div.pending").size() == 0) {
                 loading = false;
                 $(JQ.find('div[data-mid]').get().reverse()).each(function() {
                     var id = $(this).attr('data-mid');
                     if (isNaN(id)) return;

                     if ($(this).find("[data-tid=message-undo-delete-btn]").size() > 0) return;

                     queue.push(this);
                     $(this).css({
                         'outline': '5px solid #blue'
                     });
                 });
             } else return;
         }


         while (true) {
             if (queue.length > 0) {
                 var item = queue.shift();
                 if (my_name && my_name != $(item).find("[data-tid=message-author-name]").text()) {
                     $(item).css({
                         'outline': '5px solid red'
                     });
                 } else {
                     $(item).css({
                         'outline': '5px solid blue'
                     });
                     mouse('contextmenu', $(item).find(".ui-chat__messagecontent>div")[0]);

                     var delete_button = JQ.find("[data-tid=message-actions-delete]")[0];
                     if (delete_button) {
                         console.log("Delete ID " + $(item).attr("data-mid"));
                         $(item).css({
                             'outline': '5px solid green'
                         });
                         my_name = $(item).find("[data-tid=message-author-name]").text();
                         console.log(["My name is: ", my_name]);
                         mouse('click', delete_button);
                         last_processed = (new Date()).getTime();
                         break;
                     } else {
                         $(item).css({
                             'outline': '5px solid red'
                         });
                     }
                 }

                 setTimeout(function() {
                     $(item)[0].scrollIntoView(false);
                 }, 100);

             } else
                 break;

         }

         if (queue.length <= 0) {
             loading = true;

         }

         if (last_processed < (new Date()).getTime() - 15000) {
             // if no new messages are coming up for 15s, consider it finished
             clearInterval(repeat);
             console.log("Finished, nothing new has come up for 15 seconds.");
         }
     }, 1000);
 })();

Copy link

ghost commented Nov 25, 2022

@alexpeter-pen lots of thanks but is to fast on delete. 429 (Too Many Requests) errors on many messages. its seems that deletes but does not delete. they appear again on refresh. To protect Microsoft Teams and its users, the bot APIs provide a rate limit for incoming request https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/rate-limit
can you please resolve it? i've tried to put a code to slow script but no lucky, i think ive only delayed the start.

@alexpeter-pen
Copy link

@geoginho I think I explained it in my comment about the rate limit, what is actually happening. I had to go twice about it over the same chat. But it is still faster than the initial solution overall and more reliable. If it bothers anyone the only way to try fixing the rate limit is to increase 1000 to something higher, but even then there is no guarantee that it will not happen.

Copy link

ghost commented Nov 25, 2022

@alexpeter-pen cool! its was easy than the solutions i was looking. its is running on 5 minutes and without messages of rate limit. i've changed 1000 to 2000. I have 3 years of messages and rate limit bothers a lot because i have to restart a 20 or 30 times from the beginning to try do delete messages that cannot be deleted before. Now on 2000 it is running and deleting all. Many thanks!!! Ops. To many requests now. I think that has a limit on total of deletes. Refresh and start again and here he goes more 5 minutes without any errors.

@ndstate
Copy link

ndstate commented Mar 31, 2023

This is great, thanks. Does anyone have something similar for Skype?

@PiotrSejbukPL
Copy link

Script stopped working and shows errors

Uncaught TypeError: Cannot read properties of undefined (reading 'ownerDocument')
and TypeError: Cannot read properties of undefined (reading 'ownerDocument')

@dsci4-hacks
Copy link

dsci4-hacks commented Jul 17, 2023

Here is an updated working version of the script as of 17/07/2023:

https://github.com/dsci4-hacks/Delete-Teams-Messages/blob/main/delete_my_teams_messages.js

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