so i tracked down the problem to:
- there are entries in mdl_grade_grades which have an itemid that does not exist in mdl_grade_items
- mdl_grade_items links grade items to the courseid of the course
- on user deletion, moodle tries to create an event that includes the courseid of the deleted grade
- if there is a grade for a user in mdl_grade_grades which does not have an associated grade_item, the courseid is NULL, thus the DB query fails, and the deletion is cancelled
- in our DB there are 47000 grades in mdl_grade_grades which do not have an associated grade_item (!)
- those 47000 grades are distributed over more than 4000 users (our user total is 16000). not only old users, but plenty of new ones (!!)
- this means that 1/4 of all the users at our site can never be deleted (!!!)
since new users are also affected, my best guess is that this is either a Moodle bug or a bug in a plugin we have installed.
Please try to verify if your site has this problem as well by following these steps:
SQL query:
SELECT mdl_grade_grades.id, itemid, userid, courseid
FROM mdl_grade_grades
LEFT OUTER JOIN mdl_grade_items
ON mdl_grade_grades.itemid = mdl_grade_items.id
WHERE mdl_grade_items.courseid IS NULL
If this query returns data records, then all of the userids in these records will be undeletable
Please report back so I can see if this is an isolated problem at our site, or a bug in some installed plugin, or a general Moodle bug!