DO \$\$BEGIN
	DELETE FROM apicollections USING api WHERE api=api.id AND api.id=$id$ AND api.datasource=$datasource$ AND api.removable=true AND api.active=false;
	DELETE FROM api WHERE id=$id$ AND datasource=$datasource$ AND removable=true AND active=false;
	IF NOT FOUND THEN
		raise exception 'Api not deleted'; 
	END IF;
END\$\$;
