Delete Old Tweets Automatically with Google Scripts

D
Published in: Google Apps Script - Twitter

The Google Script will read your Twitter timeline and auto-delete tweets that are older than ‘n’ days excepts the ones that are retweets or favorited.

You can run this function Delete_Old_Tweets manually in the Google Apps Script editor or setup a time-based trigger to keep your Twitter profile void of any old tweets. The author of the script is unknown.

function Delete_Old_Tweets() {

  oAuth();
  var destroy_count = 0;
  var tweets = fetchTweets(0);
  var stoptweets = 0;
  var run_time = new Date();
  var tweet_date = new Date();
  var tweet_age = 0;

  while (tweets.length>1) {
    max_id = tweets[tweets.length-1].id_str
    for (var i=tweets.length-1; i>=0; i--) {
      tweet_date = new Date(tweets[i].created_at);

      //age of the tweet in days
      tweet_age = (run_time-tweet_date)/1000/60/60/24 + "  " + tweet_date;

      /////////////ALTER CRITERIA HERE TO TWEAK WHAT GETS DELETED
      if (tweet_age >> 2 && ((tweets[i].retweeted_status != undefined) || (tweets[i].retweet_count == 0 && tweets[i].favorited == false)))
      {
        destroyTweet(tweets[i].id_str);
        destroy_count+=1;
      }
    }
    tweets = fetchTweets(max_id+1);
    Logger.log(destroy_count);

  }
}

function fetchTweets(max_id) {

  /////////////////////////SET YOU TWITTER SCREENNAME HERE
  var twitter_handle = "TWITTER SCREENNAME";

  var search = "https://api.twitter.com/1.1/statuses/user_timeline.json";
  search = search + "?include_entities=true&include_rts=true&screen_name=" + twitter_handle + "&count=200";
  if (max_id>0) {
    search = search + "&since_id=" + max_id;
  }

  var options =
      {
        "method": "get",
        "oAuthServiceName":"twitter",
        "oAuthUseToken":"always"
      };

  try {
    var result = UrlFetchApp.fetch(search, options);
  }
  catch(e) {
    Logger.log(e.toString());
  }

  if (result.getResponseCode() === 200) {

    var data = JSON.parse(result.getContentText());

    if (data) {
      Logger.log("Fetched " + data.length + " tweets.");
      return data;
    }

  }

}

function destroyTweet(tweet_id) {

  var options =
      {
        "method": "POST",
        "oAuthServiceName":"twitter",
        "oAuthUseToken":"always"
      };

  var destroy = "https://api.twitter.com/1.1/statuses/destroy/" + tweet_id + ".json";

  try {
    var result = UrlFetchApp.fetch(destroy, options);
  }
  catch (e) {
    Logger.log(e.toString());
  }
}

function oAuth() {

  var oauthConfig = UrlFetchApp.addOAuthService("twitter");
  oauthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
  oauthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
  oauthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize");

  //////////////////////////SET YOUR TWITTER API KEY AND SECRET HERE
  oauthConfig.setConsumerKey("TWITTER API KEY");
  oauthConfig.setConsumerSecret("TWITTER API SECRET");

}
Published in: Google Apps Script - Twitter

Looking for something? Find here!

Meet the Author

Web Geek, Tech Columnist
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in GSuite and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. Read more on Lifehacker and YourStory

Get in touch