Skip to content

Instantly share code, notes, and snippets.

@carlosrojaso
Forked from timwco/delete.md
Last active January 9, 2023 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosrojaso/a3871f0f1063c62f6871d756ffca0683 to your computer and use it in GitHub Desktop.
Save carlosrojaso/a3871f0f1063c62f6871d756ffca0683 to your computer and use it in GitHub Desktop.
LinkedIn: Delete Messages (April 2020)

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 will happen eventually.

Last tested & verified working on: April, 07, 2021

Special Thanks to @lifedup for the updated script.

Setup & Cavets

  1. You can run this on the messages page.
  2. 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.
  3. 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.

How

  1. Go to the messages screen: LinkedIn Messages
  2. Open up your Chrome Console*
  3. Paste the following in the console, and keep an eye on the console.

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

/**
 * 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();
@duaneking
Copy link

This does not work. I refreshed the page and they still exist, and its throwing exceptions like crazy.

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