Select to view content in your preferred language

removeLayer require some kind of refresh?

1677
6
Jump to solution
10-21-2021 09:48 AM
RandallBrown
Emerging Contributor

ArcGIS Pro 2.8.3

The following lines seem to "technically" remove a layer from my map and save the project, but I still see the layer in the contents of my map until I close the project and re-open it.  Do I need to refresh the contents somehow?

Thanks,

        aprxMap.removeLayer(layer)
        aprx.save()
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

since you are running it from a tool, try

activeMap

see the 4th paragraph in

ArcGISProject—ArcGIS Pro | Documentation

More importantly, since you are using a tool, this reference provides some guidance on whether to reference the project on disk or if the project is open, to use CURRENT

Guidelines for arcpy.mp—ArcGIS Pro | Documentation

that may illuminate some differences, since changes to projects aren't persisted until projects are saved or saved to a copy


... sort of retired...

View solution in original post

6 Replies
DanPatterson
MVP Esteemed Contributor

python window? python notebook? separate python IDE? tool in a toolbox?

and

There are many is properties that allow you to determine if a layer fits into a general category: is3DLayer, isFeatureLayer, isGroupLayer, isRasterLayer, and isServiceLayer

and remove is from layer files or the map class

LayerFile—ArcGIS Pro | Documentation

Map—ArcGIS Pro | Documentation

there is no refresh in arcgis pro's python


... sort of retired...
0 Kudos
RandallBrown
Emerging Contributor

This is part of a script tool in a toolbox.   The method I am using is removeLayer on the map class.

0 Kudos
RandallBrown
Emerging Contributor

Here is a bit more of the code:

import sys, string, os, arcpy, time
from arcpy import env, mp

aprx = mp.ArcGISProject(aprxPath)
aprxMap = aprx.listMaps()[0]
for layer in aprxMap.listLayers():
    if layer.name == 'myLayer':
        aprxMap.removeLayer(layer)
aprx.save()

DanPatterson
MVP Esteemed Contributor

since you are running it from a tool, try

activeMap

see the 4th paragraph in

ArcGISProject—ArcGIS Pro | Documentation

More importantly, since you are using a tool, this reference provides some guidance on whether to reference the project on disk or if the project is open, to use CURRENT

Guidelines for arcpy.mp—ArcGIS Pro | Documentation

that may illuminate some differences, since changes to projects aren't persisted until projects are saved or saved to a copy


... sort of retired...
RandallBrown
Emerging Contributor

It just started working for me the way I had it written.  Could be it was only not updating the layers when I was debugging in VSCode, but I could swear I had tested it from within AGPro as well.  Thanks for your help!

0 Kudos
DanPatterson
MVP Esteemed Contributor

it won't do anything in a separate python ide, it either has to be the python window, notebook or a toolbox script


... sort of retired...
0 Kudos