Previously template->delete_page() deleted the corresponding mdl_customcert_pages row, and then called element->delete() for each element on the page. But the latter then called
element_deleted::create_from_element() which relied on that mdl_customcert_pages row to obtain the contextid for the event.
Now element->delete() is called for each element on the page, then the mdl_customcert_pages row is deleted.
Previously template->delete_page() deleted the corresponding mdl_customcert_pages row, and then called element->delete() for each element on the page. But the latter then called element_deleted::create_from_element() which relied on that mdl_customcert_pages row to obtain the contextid for the event.
Now element->delete() is called for each element on the page, then the mdl_customcert_pages row is deleted.