We had a problem generating courses for the Spring 2021 term using the
local_course_template
plugin. I missed in the documentation that you should add $CFG->defaultblocks_override = '';
to your config.php file
so we created duplicate blocks in hundreds of courses. I looked around for a CLI or administrative tool, or moosh script,
that could identify and bulk delete these duplicate blocks but could not find one. I was wary to delete blocks by direct
database queries but I picked out a particular one and tested it—it seemed to work. It was super helpful to read
the blocks entity-relationship diagram
to figure out a) the impact of database deletions (luckily block_instances
is connected
only to the context
table which made me feel safe to test this out) and b) design the queries here.
The general process: run a query to identify courses with duplicate blocks in them, use that as a sub-query in another query to identify blocks that are potentialy members of duplicate sets within those courses, finally run a python script to identify the identifiers of duplicates to be deleted. Then the final step is simply:
DELETE FROM {prefix}_block_instances
WHERE id IN (...comma-separated output of the python script...)