Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm having some issues with a Python script for ArcGIS 10. The idea is to delete points from a single shapefile according to the values of two columns ("Tracks" and "Tracks_1"). Therefore, I want to:

  1. Select features who share a certain ID
  2. Unselect some of the features depending on their values
  3. Delete the remaining selection from the shapefile
  4. Proceed with the next ID


import arcpy
env.overwriteOutput = True
arcpy.MakeFeatureLayer_management(centroids_thinned, "centroids_thinned_layer")
for row in rows:
    ID_centroids = row.getValue("FID_centro")
    ID_buffer = row.getValue("FID_cent_1")
    Tracks_centroids = row.getValue("Tracks")
    Tracks_buffer = row.getValue("Tracks_1")
    if ID_buffer == ID_centroids:
        arcpy.SelectLayerByAttribute_management ("centroids_thinned_layer", "NEW_SELECTION", "FID_centro = " + str(ID_buffer))
        arcpy.SelectLayerByAttribute_management ("centroids_thinned_layer", "REMOVE_FROM_SELECTION","FID_centro = FID_cent_1")
        arcpy.SelectLayerByAttribute_management ("centroids_thinned_layer", "REMOVE_FROM_SELECTION","Tracks_1 > " +  str(Tracks_centroids))
        count = int(arcpy.GetCount_management("centroids_thinned_layer").getOutput(0))
        if count > 0:
                print str(count) + " Attributes Deleted."

The selection process works fine but for some reason those selected features are not deleted from the attribute table. How can i solve this problem?

share|improve this question
Are the 5 lines after if ID_buffer == ID_centroids: really at the same indentation level as that statement? – nmpeterson Apr 17 '13 at 16:20
No. That must have happened when i copied the code. I'm gonna correct that. Thanks. – C.Riedel Apr 17 '13 at 16:48
When you say the selection process works fine, how exactly do you know that? Is it simply not crashing, or are you actually inspecting the values of count? – nmpeterson Apr 17 '13 at 19:34
Yes, I was inspecting the count values. – C.Riedel Apr 18 '13 at 16:07
up vote 4 down vote accepted

I don't have the time right now to revise your code, but I think your problem is that you're trying to delete features from the very same shapefile you're in the middle of looping through (albeit as a feature layer).

I would suggest that you only call arcpy.DeleteFeatures_management() once, after the search cursor has gone through the entire thing; you can keep track of all of the features to delete as the cursor progresses using a python list, and then construct a query to re-select and delete them all in one go at the end.

share|improve this answer
Thanks. That did the trick. DeleteFeatures doesn't work when it's within a SearchCursor. – C.Riedel Apr 18 '13 at 16:09

Like @nmpeterson said, it's likely because you're doing this: open SearchCursor on centroids_thinned.shp, you run DeleteFeatures while your searchCursor is still open on centroids_thinned.shp.

I don't know exactly what your data looks like, but a common approach is to open a arcpy.da.UpdateCursor and get the records sorted based on some fields (for you it's Tracks & Tracks_1).

sql_clause  = ('','ORDER BY ID_centroids, ID_buffer ')

Then basically keeping track of when the values change from the previous record to determine if it meets your "delete" criteria or not.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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