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

Amit Agarwal

Google Developer Expert, Google Cloud Champion

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

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

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

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

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

We will never send any spam emails. Promise.