Select to view content in your preferred language

Update Cursor for Deleting Rows in Hosted Layer

10-14-2022 09:27 AM
New Contributor II

I'm trying to delete rows in a hosted layer by searching my local layer to feed an update cursor. I'm hitting an error claiming "AttributeError: 'Polygon' object has no attribute 'FACILITYID'". Below is my current code:


arcpy.env.overwriteOutput = True

# Create GIS object
gis = GIS('', username, password)
print("Logged in as " + str(

#lyr that will be search cursored
lyrSearch = r"C:\blahblah\JOINED_STRUCTURES_1"

#Hosted Layer Item ID
fsItemId = "2e1f37aab41746afacfcf7c8534384e8" 

#lyr that will be inserted into
lyrInsert = gis.content.get(fsItemId)
#needs to point directly to the layer in the Feature Service
tableInsert = lyrInsert.layers[0].url
print("Got Feature Service")

#a list of the fields needed to be loaded

#Update function
uCurs = arcpy.da.UpdateCursor(tableInsert, 'FACILITYID')

#cursor for searching and then feeding uCurs

n = 0
with arcpy.da.SearchCursor(lyrSearch, fieldNames) as sCurs:
    for row in sCurs:
        if row[0].FACILITYID == sCurs:
            n +=1

print("Deleted " + str(n))

If I remove the .FACILITYID in line 30 and just have "row[0] == sCurs:", then the script will run through, but not delete anything. Any suggestions?

0 Kudos
3 Replies
MVP Esteemed Contributor

Couldn't you just use FeatureLayer.delete_features() instead? If you have a reliable method to identify your deletes, passing a list of OIDs to that method is very simple.

- Josh Carlson
Kendall County GIS
New Contributor II

So I would need to create a list containing the FACILITYID from the JOINED_STRUCTURES_1 layer and feed that into the FeatureLayer.delete_features() command as the where clause right?

0 Kudos
MVP Esteemed Contributor

Yeah, you could create a list, but it needs to be in valid SQL, so you'd need to convert it to something you can pipe into "some_field IN('list','of','values')".

delete_where = f"""FACILITYID IN({','.join(["'" + i + "'" for i in your_list])})"""

Spits out something like:

FACILITYID IN('a','list','of','values')
- Josh Carlson
Kendall County GIS
0 Kudos