Skip to content

Instantly share code, notes, and snippets.

@aymericbeaumet
Last active February 18, 2025 03:06
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)
@hamzah1P16
Copy link

hamzah1P16 commented Jan 25, 2025

This script is for handling the case where tweets appear in your "Likes" list but do not have the red heart symbol (i.e., they appear unliked). It ensures these tweets are properly unliked by first liking them and then unliking them.


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

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

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

async function removeAll() {
  let count = 0;
  while (count < 500) {
    try {
      // Find the next tweet with a "like" button (appears unliked but is in liked tweets list)
      const likeButton = nextLike();
      if (likeButton) {
        // Scroll the tweet into view to ensure it's interactable
        likeButton.scrollIntoView({ behavior: 'smooth', block: 'center' });
        await wait(1000); // Wait for the tweet to be in view

        // Like the tweet first to reset its state
        likeButton.focus();
        likeButton.click();
        console.log(`Liked tweet to reset state`);
        await wait(1000); // Wait 1 second for the like action to complete

        // Now find the "unlike" button and unlike the tweet
        const unlikeButton = nextUnlike();
        if (unlikeButton) {
          unlikeButton.focus();
          unlikeButton.click();
          console.log(`Unliked ${++count} tweets`);
          await wait(1000); // Wait 1 second between unlikes
        }

        // Remove the processed tweet from the DOM to avoid reprocessing it
        const tweetElement = likeButton.closest('[data-testid="tweet"]');
        if (tweetElement) {
          tweetElement.remove();
        }
      } else {
        // If no more "like" buttons are found, scroll to load more tweets
        window.scrollTo(0, document.body.scrollHeight);
        await wait(2000); // Wait 2 seconds for more tweets to load
      }
    } catch (error) {
      console.error('An error occurred:', error);
      break;
    }
  }
  console.log('Finished, count =', count);
}

removeAll();

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