Saturday, October 4, 2025

DeleteAllMails

Multiple Code Blocks

Copy appsscript

Click or tap to copy code

function deleteAllEmails() {
  // Process in batches to avoid timeout
  const batchSize = 100;
  let threads;
  
  do {
    // Get threads in batches
    threads = GmailApp.getInboxThreads(0, batchSize);
    
    if (threads.length > 0) {
      GmailApp.moveThreadsToTrash(threads);
      Logger.log('Moved ' + threads.length + ' threads to trash');
      
      // Small delay to avoid rate limits
      Utilities.sleep(1000);
    }
  } while (threads.length > 0);
  
  // Now delete from other labels (Sent, Drafts, etc.)
  deleteFromAllLabels();
  
  Logger.log('All emails moved to trash!');
}

function deleteFromAllLabels() {
  const labels = ['sent', 'drafts', 'spam'];
  
  labels.forEach(label => {
    let threads;
    do {
      threads = GmailApp.search('in:' + label, 0, 100);
      if (threads.length > 0) {
        GmailApp.moveThreadsToTrash(threads);
        Logger.log('Moved ' + threads.length + ' threads from ' + label + ' to trash');
        Utilities.sleep(1000);
      }
    } while (threads.length > 0);
  });
}

// Alternative: Delete everything using search
function deleteAllEmailsWithSearch() {
  const batchSize = 100;
  let threads;
  
  do {
    // Search for all emails
    threads = GmailApp.search('*', 0, batchSize);
    
    if (threads.length > 0) {
      GmailApp.moveThreadsToTrash(threads);
      Logger.log('Moved ' + threads.length + ' threads to trash');
      Utilities.sleep(1000);
    }
  } while (threads.length > 0);
  
  Logger.log('All emails moved to trash!');
}

// To permanently delete (empty trash)
function emptyTrash() {
  // Note: There's no direct API to empty trash
  // You'll need to manually empty trash in Gmail
  Logger.log('Please manually empty trash in Gmail settings');
}

No comments:

Post a Comment