I'm struggling to understand how to manage print layouts using python for QGIS. For example, I am starting with Zero exisiting print layouts in my layout manager. I create a new print layout and add a label to it with this code:

Notice, I created a variable "layouts_list" which stores my layouts and for loop to check the names of existing layouts, if the name equals the new layout I want to create, I remove before creating a new one. Also, I print the layout names from the layout manager before and after checking the layout names.

However, it seems the .remove() method doesn't actually remove a layout from the layout manager.

J.Monticolo suggested I try the .removeLayout() method. So I modified my for loop like this:

for layout in layouts_list:
if layout.name() == layoutName:
manager.removeLayout(layout)

This does actually remove the layout from the layout manager. But I'm met with this error:

This makes sense. I did delete the layout. But a new layout is not created. They should be as my remaining code makes a new layout and adds a title to it.

Why does this error message stop the code, before creating a new layout?