Delete Old Tweets Automatically with Google Scripts

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() {

  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;

      if (tweet_age >> 2 && ((tweets[i].retweeted_status != undefined) || (tweets[i].retweet_count == 0 && tweets[i].favorited == false)))
    tweets = fetchTweets(max_id+1);


function fetchTweets(max_id) {

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

  var search = "";
  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",

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

  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",

  var destroy = "" + tweet_id + ".json";

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

function oAuth() {

  var oauthConfig = UrlFetchApp.addOAuthService("twitter");

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

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script - Twitter

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

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. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube