Deleting a calendar event with a python script

Nextcloud version (eg, 20.0.5): 27.1.6
Operating system and version (eg, Ubuntu 20.04): Debian 12
Apache or nginx version (eg, Apache 2.4.25): nginx 1.25.4
PHP version (eg, 7.4): 8.2

The issue you are facing:

Hi,

Not sure if this is the right forum to ask but I am not aware of a better place.

I am trying to understand why I cannot delete an event from a nextcloud calendar with a python script. Copying events from an ics calendar into a nc calendar is possible. I now would simply like to try to delete events in a nc calendar but all I get is this:

requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://whatever.com/remote.php/dav/calendars/rscw/u13/e1eec842-af4b-48fc-b904-f9fdd3f51964.ics

… but I know that the uid does exist:

MariaDB [nextcloud]> select count(*) from oc_calendarobjects where uid="e1eec842-af4b-48fc-b904-f9fdd3f51964";
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0,003 sec)

Thank you for any idea helping to solve this.
kr,
Martin

I played around with the caldav package:


with caldav.DAVClient(url=url, username=user, password=pw) as client:
    my_principal = client.principal()
    main_cal = my_principal.calendars()[1]
    my_event = main_cal.add_event(dtstart=datetime.datetime(2024,3,10,8),
                                       dtend=datetime.datetime(2024,3,11,1),
                                       summary="test-event2")
    test = main_cal.event_by_uid(my_event.id)
    test.delete()

main_cal was the second element in the list of calendars (my_principal.calendars()). I was able like this to create an event and then delete it again.

I forgot to mention … i was using icalendar like this …

...
    existing_uids = [bytes.decode(e['UID'].to_ical()).replace('\'', '')
                     .replace('/', 'slash') for e in target_cal.walk('VEVENT')]
    for euid in existing_uids:
        if euid in existing_uids:
            delete_url = '%s/%s.ics' % (base_url, euid)
            logging.info('Deleting event with UID: %s', euid)
            r = requests.delete(delete_url, auth=(username, password))
        if r.status_code == 204:
            logging.info('Deleted event with UID: %s', euid)
        else:
            logging.error('Failed to delete event with UID %s. Status code: %d', euid, r.status_code)
            try:
                response_content = r.content.decode('utf-8')
                logging.error('Response content: %s', response_content)
            except UnicodeDecodeError:
                logging.error('Failed to decode response content as UTF-8')
            logging.error('Response headers: %s', r.headers)
            r.raise_for_status()
    logging.info('  Done.')
...

I guess I will give caldav a try.

The if statement is obviously always true, isn’t it?

If it does all the decoding already, it shortens your code a bit.

Yes, absolutely. I blame this on my lazyness.