I'm looking into
MDL-31914 and have noticed that the code to delete a module is quite separated and there's room for error.
- Some of the code is in course/mod.php (e.g. calling the $cm->modname . "_delete_instance" function, deleting area files, trigger the mod_deleted event, log the event, delete_mod_from_section, rebuild_course_cache.
- Some of this is duplicated in the AJAX delete handler (course/rest.php)
- The remainder (deleting course modules, course completion, events, etc) is handled in course/lib.php::delete_course_module.
IMHO this should all be moved to the delete_course_module() function which already takes a course_module ID