final public static function delete_all_for_context($contextid, $downloadcontents = true) {
        global $DB;
        $repoids = $DB->get_fieldset_select('repository_instances', 'id', 'contextid = :contextid', array('contextid' => $contextid));
        if ($downloadcontents) {
            foreach ($repoids as $repoid) {
                $repo = repository::get_repository_by_id($repoid, $contextid);
                $repo->convert_references_to_local();
            }
        }
        cache::make('core', 'repositories')->purge();
        $DB->delete_records_list('repository_instances', 'id', $repoids);
        $DB->delete_records_list('repository_instance_config', 'instanceid', $repoids);
    }