Automatically Delete Old Google Calendar Events

Here’s a script that deletes the old events on all your Google calendars.

Deleting calendar events that are 30 or more days in the past from today.

All the code can be found on my GitHub.

https://github.com/zjrubin/clean_calendar

Background

I use Google Calendar to schedule and track my various commitments. I've been using Google Calendar since about 2014 and I've noticed that Google has tons of data on my day-to-day schedule and whereabouts. I decided to write this script to automatically delete old Google Calendar events because I don't want Google to have any more data on me than what is relevant for the services I use.

The Python Script

The business logic of the python script is straightforward. The main function gives us a high level overview of what the script does:

 1def main():
 2    creds = handle_authentication()
 3
 4    service = build("calendar", "v3", credentials=creds)
 5
 6    calendars = get_calendars(service)
 7
 8    for calendar in calendars:
 9        events = get_old_events(service=service, calendar_id=calendar["id"])
10
11        delete_events(service=service, calendar=calendar, events=events)
  • handle_authentication() acquires authentication credentials to access your calendars. You will need to log into your Google account for the script to proceed.
  • build() creates the service object that we can use to interact with the Google Calendar API
  • get_calendars() gets a list of all the calendars you have write access to For each calendar, get_old_events() gets a list of all the events on that calendar that are 30 or more days in the past from today.
  • delete_events() deletes each of the old events one-by-one. It also prints out the timestamp and summary of each event it deletes.

Setup & Running the script

The repository includes the file README.md that has information on how to setup and run the script. Please refer to this README for the most up-to-date information on how to setup and run this script.

README

An example of deleting old events from multiple calendars. The URL at the top of the image was used for a 1-time authentication with the Google Calendars API.

These calendars have already been cleaned of old events.

Running the Script Automatically

In the repository I included clean_calendar.sh, a shell script to activate the python virtual environment with the required packages for the script and run the script:

1#!/usr/bin/env bash
2
3set -euo pipefail
4# set -x
5
6DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
7source "$DIR/env/bin/activate"
8
9python3 "$DIR/clean_calendar.py"

This script assumes that the virtual environment is in env/ , a subdirectory of the repository.

I then added an entry to my crontab to call this shell script once a day:

1# Remove old Google Calendar events
20 15 * * * /home/zach/.dotfiles/scripts/user_scripts/clean_calendar/clean_calendar.sh

With this entry, cron will run this script to delete old Google calendar events once a day at 3PM.

Journal output showing that cron is running the script every day (as long as the computer is on).