Skip to content

Instantly share code, notes, and snippets.

@aymericbeaumet
Last active November 23, 2024 09:49
Show Gist options
  • Save aymericbeaumet/d1d6799a1b765c3c8bc0b675b1a1547d to your computer and use it in GitHub Desktop.
Save aymericbeaumet/d1d6799a1b765c3c8bc0b675b1a1547d to your computer and use it in GitHub Desktop.
[Recipe] Delete all your likes/favorites from Twitter

Ever wanted to delete all your likes/favorites from Twitter but only found broken/expensive tools? You are in the right place.

  1. Go to: https://twitter.com/{username}/likes
  2. Open the console and run the following JavaScript code:
setInterval(() => {
  for (const d of document.querySelectorAll('div[data-testid="unlike"]')) {
    d.click()
  }
  window.scrollTo(0, document.body.scrollHeight)
}, 1000)
@softyoda
Copy link

Hi, is there only ways to delete them, not download them somewhere ? I just want to ctrl+f among my 10000+ liked tweet, but it is impossible, the infinite scrool broke after 200 loaded tweets, and I think it's by design.

@nafal9aadi
Copy link

nafal9aadi commented Jun 3, 2024

It takes new frontend version and use this code please

setInterval(() => {
  for (const d of document.querySelectorAll('button[data-testid="unlike"]')) {
    d.click()
  }
  window.scrollTo(0, document.body.scrollHeight)
}, 5000)

What do you mean by a "new frontend version" ?

@BrianxTu
Copy link

BrianxTu commented Aug 6, 2024

setInterval(() => {
  const d = document.querySelector('button[data-testid="unlike"]')
  if(d) {
    d.click()
  } else {
    window.scrollTo(0, document.body.scrollHeight)
  }
}, 1000)

worked for me.

@owquresh
Copy link

owquresh commented Aug 7, 2024

What is the limit on this what if you have thousands of likes? I want to start from 0.

@jcoding09
Copy link

To speed up this code while avoiding rate-limiting, consider decreasing the wait time incrementally. One approach is to reduce the time between unlikes initially and increase it if we start approaching Twitter's rate limits. We can also remove the additional wait every 50 tweets. However, this comes with a higher chance of hitting rate limits, so it’s important to adjust the timing carefully.

Below code with shorter delays that increase only if a rate-limiting error is detected. This setup reduces the wait to 3 seconds between actions and increases it only if an error occurs.

function nextUnlike() {
  return document.querySelector('[data-testid="unlike"]');
}

function wait(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function removeAll() {
  let count = 0;
  let next = nextUnlike();
  let waitTime = 3000; // Start with a shorter wait time of 3 seconds

  while (next && count < 1200) {
    try {
      next.focus();
      next.click();
      console.log(`Unliked ${++count} tweets`);
      await wait(waitTime); // Initial wait time of 3 seconds
      next = nextUnlike();

      // If no unlike button is found, scroll to load more
      if (!next && count < 1200) {
        window.scrollTo(0, document.body.scrollHeight); 
        await wait(5000); // Shorter wait for loading more tweets
        next = nextUnlike();
      }
    } catch (error) {
      console.error('An error occurred:', error);
      waitTime = Math.min(waitTime * 2, 60000); // Exponentially increase wait time if an error occurs
      console.log(`Rate limit hit? Increasing wait time to ${waitTime / 1000} seconds.`);
      await wait(waitTime); // Wait before retrying
    }
  }

  if (next) {
    console.log('Finished early to prevent rate-limiting');
  } else {
    console.log('Finished, count =', count);
  }
}

removeAll();

Explanation:

  1. Shorter Initial Wait Time: The initial wait time is set to 3 seconds instead of 10.
  2. Dynamic Wait Time: The wait time doubles each time an error occurs, up to a maximum of 1 minute. This approach helps slow down only if there’s a rate-limiting risk.
  3. Reduced Scroll Wait: The scroll wait is reduced to 5 seconds, making it quicker to load additional tweets.

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