Hello,
I am trying to remove a set of layers from a web map and add them back later. Since the layers consist of a mix of feature services and image services, I feel the easiest way to do this would be by manipulation the json of the web map. When I use AGOL assisstant, I am able to achieve the above, however when I try to do the same using ArcGIS Python API, it does not work.
I am using the code below to achieve the above, though post executing the update command I get 'True', the layers are not removed from the web map. I have also attached screenshot for reference
web_map = source.content.get(itemid)
data = web_map.get_data()
rem_j = []
for i in data['operationalLayers']:
if i['url'].find('mygishub') == -1:
rem_j.append(i)
data['operationalLayers'].remove(i)
web_map.update(item_properties=data)

I have also tried to achieve the same by using the WebMap object, but it still does not work. Have attached screenshot and code for reference.
web_map = source.content.get(itemid)
web_map_obj = WebMap(web_map)
rem_j = []
for i in web_map_obj.layers:
if i['url'].find('mygishub') == -1:
rem_j.append(i)
web_map_obj.layers.remove(i)
web_map_obj.update()

Note: I have not used the add_layer and remove_layer methods here. I am aware that the remove layer method accepts a layer object (i.e., webmap.layers[id]), this approach works for removing layers but when it comes to adding a mixture of feature services and image services as layers using their urls, I have to find the type of service and then create a FeatureLayer or ImageryLayer object accordingly for each url and finally add them to the webmap, which I feel is not very efficient as I have a large number of layers.