I am making a new map via code that shares similarities with the map I currently have open. I'm trying to generate Layer objects without automatically adding them to the new map as I want to modify them before finally adding them to a group layer. When I create a new layer object that has the same data path and name as a layer on my original map, the layer on the original map disappears for some reason. Why is my original map being modified when I don't seem to be interacting with it during layer creation? I don't want to copy the original layer at all, I want to create a new layer that just happens to have the same source and name.
p = arcpy.mp.ArcGISProject('current')
mp = p.createMap('test map')
arcpy.env.addOutputsToMap = False
# checking that the original layer is still there. Throws no exception.
original_map = p.listMaps('original map')[0]
original_layer = original_map.listLayers('layer name')[0]
# ----------
new_layer = arcpy.management.MakeFeatureLayer('url_of_feature_service', 'layer name').getOutput(0)
# checking that the original layer is still there again. Throws exception; can't find layer anymore
original_map = p.listMaps('original map')[0]
original_layer = original_map.listLayers('layer name')[0]
# ----------
mp.addLayerToGroup(mp.listLayers('group layer')[0], new_layer)
as an addendum, I'm using MakeFeatureLayer() as it was the answer i found via Google on how to create a Layer object from a portal data source without having to simultaneously add it to the map. The documentation doesn't suggest this should actually work as the arg description says it requires a Layer or LayerFile, not a string. Maybe this is the source of the issue? Is it somehow using the provided URL to return the first layer it finds with that data source?