Delete Old Tweets Automatically with Google Scripts

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');
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫢🏻