1

I would like to delete all rows which are joined to a layer from a csv table. Only the joined records which match (keep only matching records) should be deleted. After the records are deleted, the join should be removed again. I used the following script, but get the error: "cannot update join table".

arcpy.env.workspace = r"D:\skript\New File Geodatabase.gdb\test"

arcpy.MakeFeatureLayer_management("fc","featurelayer")

arcpy.AddJoin_management("featurelayer","OBJECTID",r"D:\skript\Errors.csv","id","KEEP_COMMON")

with arcpy.da.UpdateCursor("featurelayer", "OBJECTID") as cursor:
for row in cursor:
    if row[0] > 0:
        cursor.deleteRow()

arcpy.RemoveJoin_management ("featurelayer", "Errors")

arcpy.CopyFeatures_management("featurelayer","result")

Is this even possible?

  • Switch your join so that the rows you want to delete are the base and the features are the joined table. Use selection instead of a cursor. The selection persists after removing join, then call delete rows. – Ben S Nadler Jun 10 '16 at 20:12
  • 1
    I'm pretty sure that ArcGIS / Arcpy cannot modify a CSV file. I've just tried it using both an update cursor and Delete Rows (as mentioned by @BenSNadler above) and it gives me an error for both. You may need to convert your CSV into an ArcGIS editable table before you can do any edits to it. – Midavalo Jun 12 '16 at 8:17
  • You are correct. Missed that detail – Ben S Nadler Jun 12 '16 at 15:03
  • @Midavalo I think your comment would be fine as an answer. – PolyGeo Sep 24 '16 at 22:44
0

I know this is an old topic, but I had a similar issue to this today and I found a different solution. I ended up storing the ids of the items I wanted to delete in a list during a search cursor, and then removing the join, selecting by ID in (list) and then deleting the features.

no_change_ids = []
length_fields = ['out_fc.OBJECTID','out_fc.Shape_Length', old_fc + '.Shape_Length', 
working_layer + '.Shape_Length']
expression = u'{} = {}'.format(arcpy.AddFieldDelimiters("out_fc", "out_fc.CHANGE_TYPE"), "'TC'")
with arcpy.da.SearchCursor(out_fc, length_fields, expression) as cursor1:
    for row1 in cursor1:
        if row1[1] == row1[2] and row1[1] == row1[3]:
            no_change_ids.append(row1[0])
            print "No length change for row with ID:"+ str(row1[0])

arcpy.RemoveJoin_management("out_fc")

expression = u'{} in ({})'.format(arcpy.AddFieldDelimiters("out_fc", "out_fc.OBJECTID"), ','.join(map(str,no_change_ids)))
arcpy.SelectLayerByAttribute_management(out_fc,"NEW_SELECTION", expression)

arcpy.DeleteFeatures_management(out_fc)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.