public static function delete_instances_as_record(array $taginstances) {
        global $DB;

        if (empty($taginstances)) {
            return;
        }

        $taginstanceids = array_map(function($taginstance) {
            return $taginstance->id;
        }, $taginstances);
        // Now remove all the tag instances.
        $DB->delete_records_list('tag_instance', 'id', $taginstanceids);
        // Save the system context in case the 'contextid' column in the 'tag_instance' table is null.
        $syscontextid = context_system::instance()->id;
        // Loop through the tag instances and fire an 'tag_removed' event.
        foreach ($taginstances as $taginstance) {
            // We can not fire an event with 'null' as the contextid.
            if (is_null($taginstance->contextid)) {
                $taginstance->contextid = $syscontextid;
            }

            // Trigger tag removed event.
            \core\event\tag_removed::create_from_tag_instance($taginstance, $taginstance->name,
                    $taginstance->rawname, true)->trigger();
        }
    }