Bulk Unenroll Students from Moodle Courses

Posted by: on Aug 21, 2009 | 4 Comments

Simple tip from a task this afternoon. Helping a school set up all their course enrollments for next year, needed a fast way to remove all students from the current course set up.

There’s no way to do this through the web front end as far as we could see, but a quick look at the database and the Moodle docs about enrollments came up with a solution. By no means am I suggesting this is the recommended way to go about this, but it worked for us so documenting here. If you are unsure about poking a database directly I would avoid it, or attempt it on a test server first.

To unenroll all students from a moodle site:

  1. Take a backup of your database
  2. Really, take a backup of your database… How bad would it be if it went wrong?!
  3. Find the mdl_role table and look for the roleid of the student role (on ours it was 5)
  4. Now, go to the mdl_role_assignments table and delete the rows with this roleid (DELETE FROM mdl_role_assignments WHERE roleid=’the correct student role id‘;)
Note – please check the comments feed on this one, the update from Shawky adds an important extra stage to the process.
  • http://twitter.com/ianmcnaught Ian McNaught

    Thanks, was looking out for a way to do this. I had wondered about this approach, but thought it might mess up the contexts table? I still don’t entirely understand how that table works, but if the little I know is correct I think this might leave a load of orphaned records. Did you suffer any problems after doing it this way?

    On a different note, did I go to school with you?! Winchester 1993 – 2008ish. Seems like we’ve ended up in the same line of work!

  • http://twitter.com/ianmcnaught Ian McNaught

    By 2008, I of course mean 1998!

  • Shawky

    thanks a lot for this , but i have to add something:
    if you wana complete the unenrolement so u don’t see the student in course enrolled users anymore
    you should delete the students recrod from this table >>> mdl_user_enrolments <<<

    NB: delete from "mdl_user_enrolments" before "mdl_role_assignments"
           because in "mdl_user_enrolments" there's no column "roleid"
          else your second query will be more complex

    • Anonymous

      Thanks for this! Will update my post to reflect this change- had noticed it on our Moodle site but hadn’t gone back to correct instructions here.