0

I made a disastrous mistake. I used Microsoft Flow to add my google calendar invites to outlook, and then my outlook calendars to google. Obviously this created a circular funhouse of calendar invites and now I have thousands of calendar events that is crashing my calendar from opening.

I'd like to mass delete all of these events. I created a Google script that deletes events with a specific subject line leveraging the the following:

function delete_events() {
  var fromDate = new Date(2019,8,16,15,0,0); 
  var toDate = new Date(2019,8,18,11,0,0);  
  var calendarName = 'my email address';
  var toRemove = 'Heather Unavailable';
  var calendar = CalendarApp.getCalendarsByName(calendarName)[0];
  var events = calendar.getEvents(fromDate, toDate,{search: toRemove});
  for(var i=0; i<events.length;i++) {
    var ev = events[i];//duplicate declaration
    if(ev.getTitle()==toRemove) {//check if the title matches
      var ev = events[i];//duplicate declaration
      Logger.log(ev.getTitle()); // show event name in log
      ev.deleteEvent();
    }
  }
}

However, I'm getting an error:

You have been creating or deleting too many calendars or calendar events in a short time. Please try again later. (line 18, file "Code")

Please help!

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Take a look at this: https://support.google.com/a/answer/2905486?hl=en. You may presently be in read only mode for a while. – Cooper Sep 17 '19 at 01:05
  • hmm, I have a paid plan though? I don't think I hit 100k, gosh I hope not. – Heather Watkins Sep 17 '19 at 02:35
  • 1
    Well that basically about 1 per second so if you did several thousand in a a few minutes your way over the quota even though you didn't hit the 100000 mark. So any way I try not to worry about it and come back in the morning and take a look. – Cooper Sep 17 '19 at 02:43
  • You have two `var ev` declarations. I marked them with duplicate comments – Cooper Sep 17 '19 at 02:45
  • You can also try using `Utilities.sleep()` between iterations, or just deleting a few hundred at a time and put the script on a timed trigger every hour. – AMolina Sep 17 '19 at 14:14
  • I'm still hitting the max and the request is failing. @amolina, i'm not a developer, would you be able to add the correct line of code in the above script? Thank you! – Heather Watkins Sep 17 '19 at 15:37
  • Hi @HeatherWatkins just wanted to check if the issue was resolved. – AMolina Sep 19 '19 at 11:33

1 Answers1

0

Try this:

function delete_events() {
  var fromDate = new Date(2019,8,16,15,0,0); 
  var toDate = new Date(2019,8,18,11,0,0);  
  var calendarName = 'my email address';
  var toRemove = 'Heather Unavailable';
  var calendar = CalendarApp.getCalendarsByName(calendarName)[0];
  var events = calendar.getEvents(fromDate, toDate,{search: toRemove});
  var ev;

  for(var i=0; i<events.length;i++) {
    for (var j = 0; j < 200; j++){
      ev = events[i];
      if(ev.getTitle()==toRemove) {//check if the title matches
        Logger.log(ev.getTitle()); // show event name in log
        ev.deleteEvent();
      }
    }
    Utilities.sleep(5000);
  }
}

This will wait 5 seconds between deleting batches of 200 events, you can play around with the number of events to delete and the wait. Alternatively you can read into Batch Requests to better manage large numbers of requests, if you do, this answer provides an example with Drive API (You would need the delete event method from the Calendar API)

AMolina
  • 1,355
  • 1
  • 7
  • 17