Was trying backup & restore with activity grade items. To do that I continuously execute remove_course_contents() to start a new restore into empty course.
It made me crazy to get this debugging output for each of the grade items in my course, until I discovered it wasn't a bug in my restore code (using wrong activities->id or so), but something in the process of deleting grade_items/grade_grades/completion stuff.
Here it's the trace:
Couldn't find course-module for module 'forum', instance '934', course '34'
line 801 of /lib/grade/grade_grade.php: call to debugging()
line 256 of /lib/grade/grade_object.php: call to grade_grade->notify_changed()
line 352 of /lib/grade/grade_item.php: call to grade_object->delete()
line 336 of /lib/grade/grade_item.php: call to grade_item->delete_all_grades()
line 95 of /lib/gradelib.php: call to grade_item->delete()
line 1575 of /mod/forum/lib.php: call to grade_update()
line 262 of /mod/forum/lib.php: call to forum_grade_item_delete()
line 4036 of /lib/moodlelib.php: call to forum_delete_instance()
line 15 of /testrestore.php: call to remove_course_contents()
The problem is that grade_grade->notify_changed() calls to get_coursemodule_from_instance() and, unluckly, the instance has been already deleted (first step on each xxxx_delete_instance() function).
It seems that grade items are perfectly deleted and it's only when completion tries to do its job when the problem appears. Perhaps you should try to get the same information straight from course_modules instead or something like that. But current order causes the debugging message to happen always.