DELIMITER $$

CREATE FUNCTION remove_bracketed_text(input_text TEXT) 
RETURNS TEXT DETERMINISTIC
BEGIN
	DECLARE output_text TEXT DEFAULT input_text;
	DECLARE previous_text TEXT;

	-- Loop until no more bracketed text is found
	REPEAT
		SET previous_text = output_text;
		SET output_text = REGEXP_REPLACE(output_text COLLATE utf8mb4_unicode_ci, '\\[[^]]*\\]', '');
	UNTIL output_text = previous_text
	END REPEAT;

	RETURN output_text;
END $$

DELIMITER ;
