Bulk deleting from Google Calendar

I use Novell Groupwise at work and up until a couple days ago used this Cool Solution to sync my Groupwise calendar to my Google Calendar.  The gist of it is that Groupwise automatically forwards every new appointment to my gmail, which automatically adds the appointment to my calendar.  This worked fine and kept me happy except for one glaring problem.  Appointments don’t automatically delete.

Over time this has gotten to be a bigger and bigger problem as I have more and more old recurring appointments that have been deleted from Groupwise, but are still happily recurring in Google Calendar.

Groupwise 8 has a new feature by which you can publish the calendar as an ICS feed.  You can subscribe to it as a separate calendar in Google Calendar and it will all show up nice and cleanly along with your other calendars.  It’s a much better solution that forwarding rules, and it will keep in sync so I don’t have delete problems.

So I turned off my forwarding rule for calendar events, published my Groupwise calendar and subscribed to it in Google Calendar.  But I still have the problem of a bunch of old recurring Groupwise events in my main Google Calendar.   Since I followed the Cool Solution exactly, each of my Groupwise events has “GWDelegate” in the description in Google Calendar.  So when I do a search in the calendar for GWDelegate all of the Groupwise events show up in the results.  This is nice and simple.  Now I just have to delete them.

One by one.  Because I can’t delete multiple items in Google Calendar.  Ugh.

Okay, new plan.  I export the calendar to an ICS file.  I run the following awk script on the ICS file, creating a new file that has all of the GWDelegate events removed:

#!/usr/bin/env ksh

awk '
# At the beginning of an event we start caching the lines until we figure out
# if we are going to keep it or drop it
 cache = 1;
 drop = 0;

# If we match the drop criteria (a line includes the work GWDelegate)
# we flag the record as to drop, we clear the cache and we stop caching
 /GWDelegate/ {
 drop = 1;
 cached_lines = "";
 cache = 0;

# If we find the end of an event we print out the cache, clear the cache, and stop caching
# print the current line if not dropping
# stop dropping and go on to the next line
 printf( "%s", cached_lines );
 cached_lines = "";
 cache = 0;
 if ( !drop )
 drop = 0;

# If we are caching we add the current line to the cache and go on to the next line
 cache {
 cached_lines = cached_lines $0 "\n";

# If we are not caching and we are not dropping the current event we print the current line
 !drop { print; }

This awk script is a modification of one I found in this forum thread, so thanks to agama for the original solution.

Now that I have a full export of my Google Calendar with the events removed that I no longer want I can delete all events from the calendar and import the modified ICS file, restoring the ones I still want.  Not quite as nice as being able to delete all search results, but it does work.  This solution should be easy to modify to search for events with any attribute, not just GWDelegate somewhere in the event.