Skip to content

Instantly share code, notes, and snippets.

@timwco
Last active July 20, 2023 15:59
Show Gist options
  • Star 51 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save timwco/d4536183c22a2f5a8c7c427df04acc90 to your computer and use it in GitHub Desktop.
Save timwco/d4536183c22a2f5a8c7c427df04acc90 to your computer and use it in GitHub Desktop.
LinkedIn: Delete Messages (June 2022)

What

LinkedIn is a valuable resource, but sometimes it sucks. One of those times is when you want to delete messages. You have to select each message one by one. It takes about 4 "clicks" to successfully delete a message.

This script should help. Since LI requires you to perform multiple steps, I decided to automate it for you. Once you initiate the script, it will run every second. If a message has the ability to be deleted, it will be. If not, it will be archived. Some "InMail" messages cannot be deleted on the web app. This script should work as long as LI doesn't change their page layout or element names, which happens often.

Last tested & verified working on: June, 10, 2022

Special Thanks to @noncent for the updated script.

* if you are not sure what this is, then this script is not for you.

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * Delete all your old notifications from Linkedin
 * 
 * Step 1 - open link https://www.linkedin.com/notifications/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter
 */
[...document.querySelectorAll('[type="trash-icon"]')].map(x => x.click());

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * URL - https://www.linkedin.com/messaging/thread/new/
 *
 * Delete all your old & archive messages from Linkedin
 *
 * Step 1 - open link https://www.linkedin.com/messaging/thread/new/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter and click yes when prompt for delete
 */
setInterval(function () {
    [...document.querySelectorAll(".msg-selectable-entity__checkbox-circle")].map(
        function (x) {
            x.click();
            setTimeout(function () {
                [...document.querySelectorAll('[type="trash-icon"]')].map(a => a.click());
            }, 1000);
        })
}, 3000);
@hammadyasir
Copy link

It actually does not delete the messages but moves to Archive and vice versa.

go to line number 10 and replace with const METHOD = 2;

@mmccarthy67
Copy link

No longer works

@skworden
Copy link

skworden commented Feb 21, 2021

You can run this on the messages page.

It automatically trys to load around 200 messages then it selects them to be deleted. You have to click the trash icon at the top after they are selected.

It sometimes skips messages or if you have a lot of them you will need to run it again. If someone wanted to fix and join the OG way with the load messages function of this it would technically delete all of the messages by itself.

const delMsgs = async () => {
  const container = document.querySelector('.msg-conversations-container ul');
  if (!container) {
    console.log('no messages - are you on the messages page?');
    return;
  }
  const loadAllMessages = async () => {
    return await new Promise((resolve) => {
      let height = 0;
      let attempts = 0;
      if (container) {
        console.log('loading messages...');
        const interval = setInterval(() => {
          const { scrollHeight } = container;
          if (scrollHeight > 20000) {
            console.log(
              'limited to around 200 messages...'
            );
            clearInterval(interval);
            resolve();
          }
          if (scrollHeight === height) {
            if (attempts >= 3) {
              console.log('messages loaded...');
              clearInterval(interval);
              resolve();
            } else {
              console.log('...');
              attempts++;
            }
          }
          height = scrollHeight;
          container.scrollTop = scrollHeight;
        }, 1000);
      } else {
        console.log('no messages');
      }
    });
  };
  await loadAllMessages();
  console.log('attempting to select all messages');
  const labels = container.getElementsByTagName('label');
  for (let i = 0; i < labels.length; i++) {
    if (labels[i]) {
      labels[i].click();
    }
  }
  console.log('Click the trash can icon at the top to delete all messages.');
  console.log('type "delMsgs()" below this and then hit enter to run again.');
};
delMsgs();

to run it again type the following the hit enter

delMsgs()

I just used this and it still works.

@bradlucas
Copy link

Thanks very much for this. I just cleared out two years worth of messages and am very happy to see a clean Inbox.

A few tips for anyone just trying this and seeing this message:

If the script gets stuck sometimes you can simply select a different message. For some reason sometimes a message will not have the archive option. Selecting another message gets the script started on this new message and most of the time it keeps going from there.

Also, you can load new messages as you see the script finishing off the current set. Click the load new messages underneath the list of messages on the left and a new batch is loaded while the script is running.

If you run out of messages refresh the page and restart the script.

Have a bit of patience and you'll have a clean Inbox soon enough.

@mfischer2196
Copy link

Anyway to make this work on it's own? I have multiple thousands of messages...

@mfischer2196
Copy link

This isn't working for me. I keep getting errors that "something went wrong" and my list isn't shrinking.

@GaryJurgens
Copy link

Hi, this is great, thank you so very much, it cleared out my inbox very quickly.

Do you have one for the Linkedin Notifications ?

thanks G

@bradcodd
Copy link

bradcodd commented Jul 9, 2021

I keep getting the error message "no messages - are you on the messages page?"

@skworden
Copy link

skworden commented Jul 19, 2021

The LinkedIn UI was updated - I added a function to the top and changed the container selector to account for their UI update - the rest was left unchanged. I tested this today and it worked as expected.

/**
 * UI changed - process to manage messages
 * Select the ... button that open sub menu for managing messages
 * Open sub menu
 * Wait for the LinkedIn UI to catch up
 * Find "link" that toggles manage messages
 * Click the "link" to manage messages
 */
const manageMessages = async () => {
  const openMessageMenu = document.querySelector(
    ".msg-conversations-container__title-row button"
  );
  console.log('Attempting to open messaging menu');
  openMessageMenu.click();
  setTimeout(() => {
    const manageConversations = document.querySelector(
      ".msg-conversations-container__dropdown-container ul div"
    );
    console.log('Attempting to enable message management');
    manageConversations.click();
  }, 1000);
};

const delMsgs = async () => {
  await manageMessages();
  const container = document.querySelector(
    ".msg-conversations-container__conversations-list"
  );
  if (!container) {
    console.log("no messages - are you on the messages page?");
    return;
  }
  const loadAllMessages = async () => {
    return await new Promise((resolve) => {
      let height = 0;
      let attempts = 0;
      if (container) {
        console.log("loading messages...");
        const interval = setInterval(() => {
          const { scrollHeight } = container;
          if (scrollHeight > 20000) {
            console.log("limited to around 200 messages...");
            clearInterval(interval);
            resolve();
          }
          if (scrollHeight === height) {
            if (attempts >= 3) {
              console.log("messages loaded...");
              clearInterval(interval);
              resolve();
            } else {
              console.log("...");
              attempts++;
            }
          }
          height = scrollHeight;
          container.scrollTop = scrollHeight;
        }, 1000);
      } else {
        console.log("no messages");
      }
    });
  };
  await loadAllMessages();
  console.log("attempting to select all messages");
  const labels = container.getElementsByTagName("label");
  for (let i = 0; i < labels.length; i++) {
    if (labels[i]) {
      labels[i].click();
    }
  }
  console.log("Click the trash can icon at the top to delete all messages.");
  console.log('type "delMsgs()" below this and then hit enter to run again.');
};
delMsgs();

@skworden
Copy link

skworden commented Jul 19, 2021

Here is a slightly more automated version. This version deletes the messages instead of you having to click the 2 buttons after selection.

The LinkedIn UI is pretty slow from an automation standpoint so there are a lot of timeouts to account for the delays. In theory you should be able to uncomment the last line and have it run automatically but, I could not test this since it deleted my remaining messages.

/**
 * UI changed - process to manage messages
 * Select the ... button that open sub menu for managing messages
 * Open sub menu
 * Wait for the LinkedIn UI to catch up
 * Find "link" that toggles manage messages
 * Click the "link" to manage messages
 */
const manageMessages = async () => {
  const openMessageMenu = document.querySelector(
    ".msg-conversations-container__title-row button"
  );
  console.log("Attempting to open messaging menu");
  openMessageMenu.click();
  setTimeout(() => {
    const manageConversations = document.querySelector(
      ".msg-conversations-container__dropdown-container ul div"
    );
    console.log("Attempting to enable message management");
    manageConversations.click();
  }, 1000);
};

const processDeleteButton = async () => {
  const deleteButton = document.querySelector(
    ".msg-bulk-actions-panel button[title='Delete']"
  );
  setTimeout(() => {
    deleteButton.click();
  }, 2000);
};

const processConfirmModal = async () => {
  await processDeleteButton();
  const confirmModal = document.getElementById("artdeco-modal-outlet");
  setTimeout(() => {
    const confirmButton = confirmModal.querySelector(
      "button.artdeco-button--primary"
    );
    confirmButton.click();
  }, 5000);
};

const delMsgs = async () => {
  await manageMessages();
  const container = document.querySelector(
    ".msg-conversations-container__conversations-list"
  );
  if (!container) {
    console.log("no messages - are you on the messages page?");
    return;
  }
  const loadAllMessages = async () => {
    return await new Promise((resolve) => {
      let height = 0;
      let attempts = 0;
      if (container) {
        console.log("loading messages...");
        const interval = setInterval(() => {
          const { scrollHeight } = container;
          if (scrollHeight > 20000) {
            console.log("limited to around 200 messages...");
            clearInterval(interval);
            resolve();
          }
          if (scrollHeight === height) {
            if (attempts >= 3) {
              console.log("messages loaded...");
              clearInterval(interval);
              resolve();
            } else {
              console.log("...");
              attempts++;
            }
          }
          height = scrollHeight;
          container.scrollTop = scrollHeight;
        }, 1000);
      } else {
        console.log("no messages");
      }
    });
  };
  await loadAllMessages();
  console.log("attempting to select all messages");
  const labels = container.getElementsByTagName("label");
  for (let i = 0; i < labels.length; i++) {
    if (labels[i]) {
      labels[i].click();
    }
  }

  console.log("Deleting selected messages...");
  console.log("To stop refresh the page...");
  await processConfirmModal();
  console.log('type "delMsgs()" below this and then hit enter to run again.');
  // delMsgs(); // uncomment to attempt to automatically rerun. Could not test this option.
};
delMsgs();

@bradcodd
Copy link

@lifedup Yup, your fix worked like a charm. Thank you!

@noncent
Copy link

noncent commented Sep 13, 2021

Thanks for the lovely script :) but here is a very short script to manage things.

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * Delete all your old notifications from Linkedin
 * 
 * Step 1 - open link https://www.linkedin.com/notifications/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter
 */
[...document.querySelectorAll('[type="trash-icon"]')].map(x => x.click());

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * URL - https://www.linkedin.com/messaging/thread/new/
 *
 * Delete all your old & archive messages from Linkedin
 *
 * Step 1 - open link https://www.linkedin.com/messaging/thread/new/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter and click yes when prompt for delete
 */
setInterval(function () {
    [...document.querySelectorAll(".msg-selectable-entity__checkbox-circle")].map(
        function (x) {
            x.click();
            setTimeout(function () {
                [...document.querySelectorAll('[type="trash-icon"]')].map(a => a.click());
            }, 1000);
        })
}, 3000);

Cheers!!

@agileandy
Copy link

Fantastic little hack....Worked a treat for me.

I don't suppose anyone has something similar to clear years worth of notifications as well?

@dthiago
Copy link

dthiago commented Feb 10, 2022

Nice...

@jkmchugh
Copy link

I used nonevent's script above. Watched as it ran in Linkedin messages window. It selected those visible on the screen, checked the delete boxes to green, blue "delete" button popped up, then appeared to execute the delete. I let it run several passes and noticed the same messages were still there. I started clicking the blue delete button manually before script did it and they vanished. I was doing 12-20 conversations per click. For a second before script ran to delete, I scrolled the message screen and increased the messages at times to 30-40, but then it went back to 12 or so. I stayed at it for about 12 minutes and Vioala! Messages empty. At one point, it seemed to stopped working, so I closed the console and refreshed the page, and reentered the script at the console and got going again. Did this 2-3 times. I think I'm a little OCD when it comes to full Inbox. Anyone else?

@CodeWizardGenius
Copy link

/**

  • Warning - this script is made for education purpose only and must be run, executed on your own risk.
  • Author is not responsible for anything.
  • Delete all your old notifications from Linkedin
  • Step 1 - open link https://www.linkedin.com/notifications/
  • Step 2 - open browsers console panel by right click and inspect
  • Step 3 - go to console tab and paste script
  • Step 4 - Hit the enter
    */
    [...document.querySelectorAll('[type="trash-icon"]')].map(x => x.click());

/**

  • Warning - this script is made for education purpose only and must be run, executed on your own risk.
  • Author is not responsible for anything.
  • URL - https://www.linkedin.com/messaging/thread/new/
  • Delete all your old & archive messages from Linkedin
  • Step 1 - open link https://www.linkedin.com/messaging/thread/new/
  • Step 2 - open browsers console panel by right click and inspect
  • Step 3 - go to console tab and paste script
  • Step 4 - Hit the enter and click yes when prompt for delete
    */
    setInterval(function () {
    [...document.querySelectorAll(".msg-selectable-entity__checkbox-circle")].map(
    function (x) {
    x.click();
    setTimeout(function () {
    [...document.querySelectorAll('[type="trash-icon"]')].map(a => a.click());
    }, 1000);
    })
    }, 3000);

@bradcodd
Copy link

Does anyone know how to change this script to Archive messages (instead of Delete)?

@bradcodd
Copy link

This worked for me (albeit with lots of page reloads and repeated script runs)...

setInterval(function () {
[...document.querySelectorAll(".msg-selectable-entity__checkbox-circle")].map(
function (x) {
x.click();
setTimeout(function () {
[...document.querySelectorAll('[type="archive-message-icon"]')].map(a => a.click());
}, 1000);
})
}, 3000);

@bradcodd
Copy link

LinkedIn gave me a HTTP 429 error (rate limited). If you have a lot of messages in your inbox do not try to archive all of them in one session!

@thetroublemaker
Copy link

Thanks for the lovely script :) but here is a very short script to manage things.

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * Delete all your old notifications from Linkedin
 * 
 * Step 1 - open link https://www.linkedin.com/notifications/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter
 */
[...document.querySelectorAll('[type="trash-icon"]')].map(x => x.click());

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * URL - https://www.linkedin.com/messaging/thread/new/
 *
 * Delete all your old & archive messages from Linkedin
 *
 * Step 1 - open link https://www.linkedin.com/messaging/thread/new/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter and click yes when prompt for delete
 */
setInterval(function () {
    [...document.querySelectorAll(".msg-selectable-entity__checkbox-circle")].map(
        function (x) {
            x.click();
            setTimeout(function () {
                [...document.querySelectorAll('[type="trash-icon"]')].map(a => a.click());
            }, 1000);
        })
}, 3000);

Cheers!!

Worked like a charm! Thank you @noncent! I'm pretty annoyed why such a basic functionality does not exist natively. I'm assuming it's some kind of marketing/behavior stunt.

@RELYVA
Copy link

RELYVA commented Jun 2, 2022

Does anybody know of a way to mass delete posts and comments from a LinkedIn account using scripts? Thanks for sharing these great tips.
Edited to add: I was advised by a prospective employer that my posts there are generally too political and that I might be well served to remove them all. Thanks for any help you might be able to offer.

@hajajm
Copy link

hajajm commented Jun 17, 2022

thanks , it worked

@definitelycarter
Copy link

I used this to bulk delete messages that are visible on the messages screen.

setInterval(async () => {
  new Array(...document.querySelectorAll('.ember-checkbox')).slice(0, 30).forEach(x => x.click());
  await new Promise(resolve => setTimeout(resolve, 1000));
  document.querySelector('[type=trash-icon]').click();
  await new Promise(resolve => setTimeout(resolve, 1000));
  document.querySelector('[aria-label="Yes, delete selected conversations"]').click()
}, 4000)

@darvinschmidt
Copy link

I would like to bulk delete messages in my inbox and archives by using a date range or anything on or before a date. Is there a script that can handle the date as a exclusionary criterion?

@mynameisneo7
Copy link

These both work:

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * Delete all your old notifications from Linkedin
 * 
 * Step 1 - open link https://www.linkedin.com/notifications/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter
 */
[...document.querySelectorAll('[type="trash"]')].map(x => x.click());

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * URL - https://www.linkedin.com/messaging/thread/new/
 *
 * Delete all your old & archive messages from Linkedin
 *
 * Step 1 - open link https://www.linkedin.com/messaging/thread/new/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter and click yes when prompt for delete
 */
setInterval(function () {
    [...document.querySelectorAll(".msg-selectable-entity__checkbox-circle")].map(
        function (x) {
            x.click();
            setTimeout(function () {
                [...document.querySelectorAll('[type="trash"]')].map(a => a.click());
            }, 1000);
        })
}, 4000);

I only changed trash-icon to trash and increased the interval from 3 seconds to 4 because the list would sometimes load a bit slow.

YMMV.

@efriedli
Copy link

And if you only want to mass-archive:

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * URL - https://www.linkedin.com/messaging/thread/new/
 *
 * Delete all your old & archive messages from Linkedin
 *
 * Step 1 - open link https://www.linkedin.com/messaging/thread/new/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter and click yes when prompt for delete
 */
setInterval(function () {
    [...document.querySelectorAll(".msg-selectable-entity__checkbox-circle")].map(
        function (x) {
            x.click();
            setTimeout(function () {
                [...document.querySelectorAll('[type="archive-message-icon"]')].map(a => a.click());
            }, 1000);
        })
}, 4000);

@jdlinux
Copy link

jdlinux commented Mar 4, 2023

Nicely done, ended up following the tweaks @mynameisneo7 made to type "trash" and just needed to click "Delete" in pop-up for however many messages were actually loaded on screen during script-run:

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * URL - https://www.linkedin.com/messaging/thread/new/
 *
 * Delete all your old & archive messages from Linkedin
 *
 * Step 1 - open link https://www.linkedin.com/messaging/thread/new/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter and click yes when prompt for delete
 */
setInterval(function () {
    [...document.querySelectorAll(".msg-selectable-entity__checkbox-circle")].map(
        function (x) {
            x.click();
            setTimeout(function () {
                [...document.querySelectorAll('[type="trash"]')].map(a => a.click());
            }, 1000);
        })
}, 4000);

@airhartm
Copy link

The notification script does not seem to do anything in my Chrome browser console now.

@spec8320
Copy link

spec8320 commented Apr 18, 2023

After some fixes working like a charm:

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * Delete all your old notifications from Linkedin
 * 
 * Step 1 - open link https://www.linkedin.com/notifications/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter
 */
[...document.querySelectorAll('[type="trash-icon"]')].map(x => x.click());

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * URL - https://www.linkedin.com/messaging/thread/new/
 *
 * Delete all your old & archive messages from Linkedin
 *
 * Step 1 - open link https://www.linkedin.com/messaging/thread/new/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter and click yes when prompt for delete
 */
setInterval(function () {
    [...document.querySelectorAll(".msg-selectable-entity__checkbox-circle")].map(
        function (x) {
            x.click();
            setTimeout(function () {
                [...document.querySelectorAll('[type="trash"]')].map(a => a.click());
				setTimeout(function () {
					[...document.querySelectorAll('[aria-label="Yes, delete selected conversations"]')].map(a => a.click());
				}, Math.floor(Math.random() * (5000 - 3000 + 1) + 3000));
            }, Math.floor(Math.random() * (5000 - 3000 + 1) + 3000));
        })
}, Math.floor(Math.random() * (7000 - 5000 + 1) + 5000));

@chrishough
Copy link

chrishough commented May 30, 2023

I had to make modifications to this, and it worked somewhat well, just does take forever with their limits

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * Delete all your old notifications from Linkedin
 * 
 * Step 1 - open link https://www.linkedin.com/notifications/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter
 */
[...document.querySelectorAll('[type="trash-icon"]')].map(x => x.click());

/**
 * Warning - this script is made for education purpose only and must be run, executed on your own risk.
 * Author is not responsible for anything.
 * 
 * URL - https://www.linkedin.com/messaging/thread/new/
 *
 * Delete all your old & archive messages from Linkedin
 *
 * Step 1 - open link https://www.linkedin.com/messaging/thread/new/
 * Step 2 - open browsers console panel by right click and inspect
 * Step 3 - go to console tab and paste script
 * Step 4 - Hit the enter and click yes when prompt for delete
 */
setInterval(function () {
    [...document.querySelectorAll(".msg-selectable-entity__checkbox-circle")].map(
        function (x) {
            x.click();
            setTimeout(function () {
                [...document.querySelectorAll('[type="trash"]')].map(a => a.click());
				setTimeout(function () {
					[...document.querySelectorAll('[aria-label="Yes, delete selected conversations"]')].map(a => a.click());
				}, Math.floor(Math.random() * (6000 - 3000 + 1) + 3000));
            }, Math.floor(Math.random() * (6000 - 3000 + 1) + 3000));
        })
}, Math.floor(Math.random() * (8000 - 5000 + 1) + 5000));```

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