Hi, as many schools start preparing for the upcoming school year. Unenrolling all users from previous courses might be necessarily for those who are using manual enrollment method.
I found there is no quick way to bulk unenroll all user from all courses. As described by Suraj there is two ways, either by (A) unenroll all users from each course (course by course). Or (B) using flat file enrollment method.
https://moodle.org/mod/forum/discuss.php?d=360163
(A) might be awfully long process. And (B) might be also long process to generate the flat file.
I am not that expert in Moodle DB, but I found a way quickly generate the CSV file from DB using phpMyAdmin. Please do not follow this process unless you know the effect of it.
First, you must assign idnumber for both courses and users. In my case, courses have an idnumber assigned, but users are not. Thus, I use the following query to assign the username as idnumber:
UPDATE `mdl_user` SET idnumber = username WHERE 1
Second, generate the flat file by executing the following query:
SELECT 'del' as actionname, mdl_role.shortname,
mdl_user.idnumber, mdl_course.idnumber
FROM
`mdl_user_enrolments`,`mdl_course`,`mdl_user`,`mdl_enrol` ,
`mdl_role_assignments`, `mdl_context`, `mdl_role`
WHERE
mdl_user.idnumber != ‘’ AND
mdl_course.idnumber != ‘’ AND
mdl_role.id = mdl_role_assignments.roleid AND
mdl_context.instanceid = mdl_course.id AND
mdl_role_assignments.contextid = mdl_context.id AND
mdl_role_assignments.userid = mdl_user.id AND
mdl_user_enrolments.enrolid = mdl_enrol.id AND
mdl_course.id = mdl_enrol.courseid AND
mdl_enrol.enrol = 'manual' AND
mdl_user.id = mdl_user_enrolments.userid
Third, download the file by going to the button of phpMyAdmin in Query results operations section, and click on export, then chose the format as CSV.
Now you have the file ready to be uploaded to the server and executed using the flat enrolment method.
If you want to unenroll only students, add the following at the end of SQL query
AND mdl_role.shortname = 'student'
Also, you could add other criteria at the end, for example, courses that under specific category.
Again, I am not that expert in Moodle DB, thus please do your own validation before you use it. Its work for me and I thought to share it.
Thanks