Skip to content

How To Delete All Twitter Likes Using Postman

API Testing Blog

Delete All Twitter Likes Using Postman

This guide will walk you through the process of deleting all your Twitter likes using Postman. While Twitter doesn’t offer a built-in feature to delete all likes at once, you can leverage their API and Postman to accomplish this task efficiently.

Understanding the Twitter API

The Twitter API provides various endpoints for interacting with Twitter data, including liking and unliking tweets. We’ll focus on the DELETE method to unfavorite (unlike) tweets using the favorites/destroy.json endpoint.

Setting up Postman

  1. Install Postman: If you haven’t already, download and install Postman from https://www.postman.com/.
  2. Create a New Request: Open Postman and click on “New” to create a new request.
  3. Authorize Your Account: To access and modify your Twitter data, you’ll need to authenticate with Twitter. Follow these steps:
    • Click on the “Authorization” tab in Postman.
    • Select “OAuth 2.0” as the authorization type.
    • Click on “Get New Access Token” and fill in the required information:
      • Client ID & Client Secret: Generate Twitter Developer Apps from https://developer.twitter.com/en/portal/dashboard and use those credentials.
      • Scopes: Select the read, write, and offline_access scopes to grant Postman access to your Twitter likes data.
    • Complete the authorization flow by signing in to your Twitter account and granting Postman the requested permissions. Your access token will be automatically added to Postman.

Deleting Individual Likes

Before deleting all likes at once, let’s explore how to delete a single like for better understanding.

  1. Construct the Request:
    • Method: DELETE
    • URL: https://api.twitter.com/1.1/favorites/destroy.json?id=YOUR_TWEET_ID (Replace YOUR_TWEET_ID with the actual ID of the tweet you want to unlike.)
    • Headers:
      • Authorization: Bearer YOUR_ACCESS_TOKEN (Use the access token you obtained in the previous step)
  2. Send the Request: Click on “Send”. If successful, you’ll receive a response with the unliked tweet data.

Example Code:

DELETE https://api.twitter.com/1.1/favorites/destroy.json?id=1460528667045797899
Authorization: Bearer AAAAAAAAAAAAAAAAAAAAA%2F1%2F%2FaF...K51

Deleting All Likes Using Postman

Now, let’s automate the process of deleting all your Twitter likes.

  1. Pagination: Twitter limits the number of tweets returned in a single API call. To retrieve all your likes, you’ll need to implement pagination.
  2. Create a Collection: In Postman, create a new collection to organize your API requests.
  3. Add a Request: Add a new request to the collection. Name it “Delete Likes” and use the following configuration:
    • Method: DELETE
    • URL: https://api.twitter.com/1.1/favorites/list.json?count=200 (We’ll start by retrieving 200 likes at a time. Adjust the count parameter if needed.)
    • Headers: Same as before (Authorization header with your access token)
  4. Add Pre-request Script: This script will handle pagination and process the likes:
    // Store the initial cursor for pagination
    pm.environment.set("cursor", "-1");
    // Define a function to delete likes based on their IDs
    function deleteLike(likeId) {
    pm.sendRequest({
    method: 'DELETE',
    url: 'https://api.twitter.com/1.1/favorites/destroy.json?id=' + likeId,
    headers: {
    'Authorization': 'Bearer ' + pm.environment.get('access_token')
    }
    });
    }
    // Function to process responses and handle pagination
    function processResponse(response) {
    const tweets = response.json();
    // Delete the likes for each tweet
    tweets.forEach(tweet => {
    pm.sendRequest({
    method: 'DELETE',
    url: 'https://api.twitter.com/1.1/favorites/destroy.json?id=' + tweet.id_str,
    headers: {
    "Authorization": 'Bearer ' + pm.environment.get("access_token")
    }
    });
    });
    // Check for pagination and update the cursor
    if (tweets.length === 200) {
    pm.environment.set("cursor", tweets[tweets.length - 1].id_str);
    pm.sendRequest(pm.request); // Send another request to fetch the next set of likes
    } else {
    // All likes have been processed
    pm.test("All likes unliked", () => {
    pm.expect(response.statusCode).to.be.oneOf([200, 204]);
    });
    }
    }
  5. Run the Collection: Click on the “Run” button to execute the “Delete Likes” request. Keep an eye on the responses; each successful response will indicate that likes have been deleted. The script will continue fetching and deleting likes until all your likes have been processed.

Explanation of the Pre-request Script:

  • pm.environment.set(“cursor”, “-1”);: Initializes a variable called “cursor” to store the cursor for pagination.
  • deleteLike(likeId): This function takes a tweet ID as input and sends a DELETE request to unlike the tweet.
  • processResponse(response): This function parses the response from the favorites/list.json endpoint and:
    • Iterates through the list of tweets and triggers a DELETE request to unlike each tweet (using deleteLike).
    • If there are more likes to be processed (the number of tweets returned is 200), it updates the “cursor” variable with the ID of the last tweet and sends a new request to retrieve the next batch of likes.
    • Once all likes are processed, a test assertion confirms that all likes have been unliked successfully.

Considerations

  • Rate Limits: The Twitter API has rate limits. Be mindful of these limits to avoid being blocked. You can adjust the count parameter in the favorites/list.json API call to control the rate of requests.
  • Security: Always handle API credentials securely. Do not store them in your Postman collection.
  • Error Handling: The pre-request script is a basic example. Consider adding error handling to catch potential issues and ensure a robust process.

Using Postman to Delete All Twitter Likes

Postman is a powerful tool for testing and interacting with APIs. By leveraging its functionality and the Twitter API, you can automate the deletion of all your Twitter likes, saving you time and effort.

Remember to adapt the provided script and example code carefully for your specific needs, and make sure you understand and follow all API guidelines and rate limits.

API Testing Blog