How to delete several calendar events ?

How to delete several calendar events ?

by Ludo M -
Number of replies: 2

Hello,

I need to delete several calendar events when deleting an activity in a course.

I got IDs of the event in an array, but I don't know if I need to use the calendar API t o delete them  with somethin like:

foreach ($events as $eventid) {

$event = \calendar_event::load($eventid);
$event->delete();

}

Or if I can do something like:

$DB->delete_records_list('event', 'id', array_keys($events));

Please can you tell what's the best way?

Average of ratings: -
In reply to Ludo M

Re: How to delete several calendar events ?

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi Ludo,

// Remove old calendar events.
if (!$DB->delete_records('event', array('modulename' => 'YOURMODULENAME', 'instance' => $YOURMODULENAME->id))) {
     $result = false;
}

Hope it's work help.

Dominique.
In reply to Dominique Palumbo

Re: How to delete several calendar events ?

by Neill Magill -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Moodle will automatically delete events that meet those criteria for you smile

If they were not created with modulename and instance parameters set as Moodle expects you probably want to follow the example as it will ensure that events are fired for the event deletions.