google-sheetsgoogle-apps-scriptgoogle-calendar-api

Optimize deletion of all events between two dates in Google Calendar


I am working on a Google Calendar project by which I am deleting old "events" and replacing them with new "events". Tanaike has graciously helped me to make use of the Google Calendar API and his Google Apps Script library to be able to add events in a single batch request here, greatly speeding up the process.

Because of the complications associated with multiple people editing multiple entries in multiple ways, I have decided to approach it by this route.

I have a script that successfully iterates through each Google Calendar event between the defined start date and end date. What I am struggling with now is to delete all events within two dates in the same manner as adding them to the Google Calendar, i.e., a single batch request. Is this a possibility?

Here is my current script (that works, but is in need of optimization):

function deleteEvents() {

  var today = new Date();
  var startDate = new Date(today.getFullYear(),today.getMonth(),today.getDate()-14);
  var endDate = new Date(today.getFullYear()+1,today.getDate());

  // ID of the Calendar to access
  var calendarID = 'xxxxxxxxxx';


  // get the Calendar
  var calendar = CalendarApp.getCalendarById(calendarID);


  // get all existing events between date range
  var existingEvents = calendar.getEvents(startDate, endDate);


  // count and log the number of found events
  var numEvents = existingEvents.length;

  // loop through found events ***************
  for (var i = 0; i < numEvents; i++) {

    // create variable for current event
    var event = existingEvents[i];

    // delete the event
    event.deleteEvent();

  }
}

Solution

  • I believe your goal is as follows.

    In this case, how about the following modification?

    Usage:

    1. Install a Google Apps Script library

    In order to use the batch request, it is required to create a request body. Ref But, I thought that it might be a bit difficult. So, I created it as a Google Apps Script library. Ref In this answer, this library is used.

    Please install BatchRequest of the Google Apps Script library. You can see how to install it here.

    2. Enable Calendar API

    Please enable Calendar API at Advanced Google services. Ref

    3. Modified script

    When your script is modified using the above library and Calendar API, it becomes as follows.

    function deleteEvents() {
      var calendarID = "###"; // Please set your calendar ID.
    
      var today = new Date();
      var startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 14);
      var endDate = new Date(today.getFullYear() + 1, today.getDate());
      var calendar = CalendarApp.getCalendarById(calendarID);
      var existingEvents = calendar.getEvents(startDate, endDate);
    
    
      // Create a request body for the batch request.
      var requests = existingEvents.map(e => ({
        method: "DELETE",
        endpoint: `https://www.googleapis.com/calendar/v3/calendars/${calendarID}/events/${e.getId().split("@")[0]}`,
      }));
    
      // Request the batch request using the created request body.
      var result = BatchRequest.EDo({ batchPath: "batch/calendar/v3", requests });
      console.log(result);
    }
    

    References: