How do I delete course enrolments in moodle in bulk?

Tags: php moodle

Moodle has a built in per course reset ability. I want to reset all my courses.


You should not reset courses this way, its too risky. Use the Course administration feature Choose the course, find the course admin. block, click the reset button, very simple. If you have only 3 courses, this takes about 30 seconds.

Sorry to bring this old post back but after a long struggle I finally got that code to work. I've tested it on a Moodle 1.9.7 enviroment


$courseids = array(8,9,11);

foreach ($courseids as &$value) {
    $data->MAX_FILE_SIZE = 8097152;
    $data->reset_start_date = 1251781200;
    $data->reset_events = 1;
    $data->reset_logs = 1;
    $data->reset_notes = 1;
    $data->reset_roles = Array(5);
    $data->mform_showadvanced_last = 0;
    $data->reset_roles_local = 1;
    $data->reset_gradebook_grades = 1;
    $data->reset_assignment_submissions = 1;
    $data->reset_forum_all = 1;
    $data->reset_quiz_attempts = 1;
    $data->id = $value;

    require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $value));

    $status = reset_course_userdata($data);

This video can help you solving your question :)
By: admin