After 14 years of calendar events (>17,000, but still well within the recommended limit of 50,000), Calendar.app finally started acting up; a month-long period refused to sync via iTunes from an iPhone to a Mac, despite desultory remedial efforts1.
Time to archive and cleanup:
Backup calendar(s) to both ICS and ICBU formats; print to PDF for good measure
Recover recalcitrant events from iPhone using iExplorer
Install icalBuddy2 version 1.8.10 via Homebrew3:brew install ical-buddy
Export all events from 2003 through 2017 using a for loop4:for i in {2003..2017}; do icalBuddy --separateByDate --noCalendarNames eventsFrom:$i-01-01 to:$i-12-31 > $i.txt; done
Combine all 2003–2017 txt files into one:awk 'FNR==1{print ""}1' *.txt > events.txt
Check for duplicates:uniq -d events.txt
and remove:uniq events.txt > events_deduped.txt
Remove auto-generated URLs for events created via iMessage or Mail:sed '/^\ \ \ \ url:/d' events_deduped.txt > events_urls_removed.txt
Delete 2003–2017 events from Calendar via Automator5: Find Calendar Events > Date ending > is before > 1/1/2018 > Delete Calendar Events6 (UPDATE: See macOS: Batch delete calendar events via Shortcuts.app)
1 namely (and mainly from How To Reset Calendar, iTunes USB Sync):
~/Library/Calendars/Calendar Cache*
and ~/Library/Caches/com.apple.iCal
)~/Library/Preferences/com.apple.iCal.plist
and ~/Library/Preferences/com.apple.iCal.helper.plist
)$ /System/Library/Frameworks/SyncServices.framework/Versions/A/Resources/resetsync.pl full
)2 Display iCal events on the command line or as Rich Text
3 to avoid a bug in the officially-hosted version (1.8.8) under OS X 10.10+.
4 Exporting all events from 2003 through 2017 like so: $ icalBuddy --separateByDate --noCalendarNames eventsFrom:'jan 1, 2003' to:'december 31, 2017'
completed without error, but stopped in 2006, hence the for loop.
5 Calendar, Automator, and AppleScript were so recalcitrant under macOS Sierra that an OS X Mountain Lion virtual machine had to be employed for the cleanup process. More on deleting old events:
6 Under macOS 10.14 Mojave, had to grant Full Disk Access to Automator (to prevent "The calendar is read only" error) and change the workflow slightly: Get Specified Calendar Items > Add > select calendar > Filter Calendar Items > Filter > events > Date ending > is before > 1/1/2019 > Delete Calendar Events > "If events are recurring, delete" > these events only.
The Calendar Exporter), but it only exports to ICS (which Calendar.app does already). Further, it only exported a few hundred events before stopping.
/mac | Apr 01, 2018