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?

რეიტინგების საშუალო: -
In reply to Ludo M

Re: How to delete several calendar events ?

by Dominique Palumbo -
Particularly helpful Moodlers სურათი 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 -
Core developers სურათი Peer reviewers სურათი 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.
რეიტინგების საშუალო: -