Skip to content

Instantly share code, notes, and snippets.

@aymericbeaumet
Last active December 19, 2024 19:13
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)
@trinlol
Copy link

trinlol commented Jan 1, 2024

Here is a version for likes, but you can specify tweets from a specific user to delete.

setInterval(() => {
  function removePostsByAuthor(authorHandle) {
    let posts = Array.from(document.querySelectorAll('.tweet'));
    posts.forEach(post => {
      let author = post.querySelector('.tweet-header-handle').textContent;
      if (author === authorHandle) {
        let likeButton = post.querySelector('.tweet-button.tweet-interact-favorite.tweet-interact-favorited');
        if (likeButton) {
          likeButton.click();
        }
      }
    });
  }

  // Call the function with the author's handle
  removePostsByAuthor('@USERNAME'); // Case sensitive (must match exactly)
  window.scrollTo(0, document.body.scrollHeight)
}, 1000)

Here is a version for retweets, but you can specify tweets from a specific user to delete.

setInterval(() => {
function removePostsByAuthor(authorHandle) {
  let posts = Array.from(document.querySelectorAll('.tweet'));
  posts.forEach(post => {
    let author = post.querySelector('.tweet-header-handle').textContent;
    if (author === authorHandle) {
      let tweetButton = post.querySelector('.tweet-button.tweet-interact-retweet.tweet-interact-retweeted');
      if (tweetButton) {
        tweetButton.click();
        let retweetMenu = tweetButton.parentElement.querySelector(".tweet-interact-retweet-menu-retweet");
        if (retweetMenu) {
          retweetMenu.click();
        }
      }
    }
  });
}

// Call the function with the author's handle
removePostsByAuthor('@USERNAME'); // Case sensitive (must match exactly)
  window.scrollTo(0, document.body.scrollHeight)
}, 1000)

Probably a better way to do this, but it works.

Must be using the old twitter extension as the above post's OldTwitter - Easier to get element IDs.

@danielwagn3r
Copy link

Made a little modification, which adds a page scroll to load futher likes in case you've got really many of them

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();
  while (next && count < 500) {
    try {
      next.focus();
      next.click();
      console.log(`Unliked ${++count} tweets`);
      await wait(count % 50 === 0 ? 60000 : 10000); // Increased wait times
      next = nextUnlike();
      if (!next && count < 500) {
        window.scrollTo(0, document.body.scrollHeight); // Scroll to the bottom of the page
        await wait(10000); // Increased wait time for more tweets to load
        next = nextUnlike(); // Get the next unlike button
      }
    } catch (error) {
      console.error('An error occurred:', error);
      break;
    }
  }
  if (next) {
    console.log('Finished early to prevent rate-limiting');
  } else {
    console.log('Finished, count =', count);
  }
}

removeAll();

@captainhook
Copy link

Is there a way to have this first like, and then unlike tweets on the Likes page? The first script here left me with over 121K formerly liked tweets still counted and sitting under the Likes section. Only liking again and then unliking again fully removes them.

I'm facing this issue now too. You could possibly run the script to first go and like a few hundred tweets and then after that a script to unlike them again. Or you'd have to create a counter around document.querySelector and loop through that.

@celine1OF1
Copy link

Screenshot 2024-01-10 091055
why arent the rest of my likes showing?

@captainhook
Copy link

Plenty of people above having the same issue with no definitive reasoning. Could be rate limiting, could be free tier limits, could be API bug, could be something else.

@h0jeZvgoxFepBQ2C
Copy link

This works fine for me:

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

@Valx01P
Copy link

Valx01P commented Jan 29, 2024

Damn twitter API, these rate limits suck, how am I supposed to delete my socially incriminating twitter history if it only let's me remove 20 likes using a script before it stops letting me, twitter needs to implement an API endpoint to just remove all likes, tweets, retweets, etc. There is no way I'm gonna be able to remove thousands of likes manually lmao

@Fodules
Copy link

Fodules commented Feb 9, 2024

This works fine for me:

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

works for me as well

@nikolaydubina
Copy link

this is incredible 👍

@nikolaydubina
Copy link

nikolaydubina commented Feb 17, 2024

I added more scripts for https://github.com/nikolaydubina/twitter-remover

  • removing tweets
  • removing retweets
  • removing replies
  • removing followers

@skibideetoilet
Copy link

is there one that likes and unlikes the tweets in your likes that for some reason are unliked but haven't been removed?

@schmutzie
Copy link

schmutzie commented Feb 22, 2024 via email

@LetyEspinosa19
Copy link

It does not work for me, I also tried this script:

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

And didn't worked also ):

Copy link

ghost commented Mar 13, 2024

Is there anyone who deletes DMs chat?

@NewWestSon
Copy link

It didn't work for me, so I tried waiting 100 milliseconds and it seemed to work, odd.
setInterval(() => {
for (const d of document.querySelectorAll('div[data-testid="unlike"]')) {
d.click()
}
window.scrollTo(0, document.body.scrollHeight)
setTimeout(pass, 100);
}, 1000)

@Anon14j1
Copy link

Anon14j1 commented Apr 5, 2024

function getAllTweetButtons() { return Array.from(document.querySelectorAll('.tweet-interact-favorite')); }

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

async function processTweet(tweetButton) { tweetButton.focus(); tweetButton.click(); // Click to like (if not liked) or unlike (if liked) await wait(1000); // Reduced wait time

// Check if the tweet is still liked, if so, click again to unlike if (tweetButton.classList.contains('tweet-interact-favorited')) { tweetButton.click(); // Click again to ensure it's unliked await wait(500); // Reduced wait time console.log('Ensured tweet is unliked'); } else { console.log('Processed tweet'); } }

async function removeAll() { let count = 0; let tweetButtons = getAllTweetButtons();

while (count < 500 && tweetButtons.length > 0) { for (let i = 0; i < tweetButtons.length && count < 500; i++) { await processTweet(tweetButtons[i]); count++; if (count % 50 === 0) { console.log('Waiting to prevent rate-limiting'); await wait(15000); // Wait to prevent rate-limiting } }

// Scroll to the bottom of the page to load more tweets
window.scrollTo(0, document.body.scrollHeight);
await wait(3000); // Wait for more tweets to load
tweetButtons = getAllTweetButtons(); // Refresh the list of tweet buttons

}

console.log('Finished, count =', count); }

removeAll();

Fix for shadow likes on feed Works with Old Twitter layouts Modify the rate limiting waiting as neccessary

Im getting a Promise error when i try this
"promise {: undefined}"

@PrinOrange
Copy link

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)

@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.

@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.

@woodenpil1ow
Copy link

Whenever i am on twitter it says i am following the three people i clicked the follow button on but whenever i click it it says to "find people to follow"
1 is that connected to me using this to delete my likes
2 if so how can i fix it and if not..... still how do i fix it

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