I have the following stand alone script but it deletes all the features in the feature class instead what I have in the query. It also only prints out that there is 9 records that meet the query criteria witch is not correct there are more than 9 records. Seems pretty straight forward but I am Not sure what I am doing wrong.
I also tried using updatecursor but I get the same results, it deletes are the features in the feature class.
TaxPar = r"C:\Temp\TaxParcels1.shp"
# Process: Make Feature Layer
query = "DXF_TEXT LIKE 'Q%' or DXF_TEXT = ' ' or DXF_TEXT IS NULL"
arcpy.SelectLayerByAttribute_management(TaxPar, "NEW_SELECTION", query)
result = arcpy.GetCount_management(TaxPar).getOutput(0)
print ('{} has {} records'.format(TaxPar, result[0]))
if int(arcpy.GetCount_management(TaxPar).getOutput(0)) > 0:
arcpy.DeleteFeatures_management(TaxPar)
sql = "DXF_TEXT LIKE 'Q%' or DXF_TEXT = '' or DXF_TEXT IS NULL"
with arcpy.da.UpdateCursor(TaxPar, 'DXF_TEXT',sql) as cursor:
for row in cursor:
cursor.deleteRow()
else:
pass
I don't see where you actually make a feature layer after line 3 in the first code block.
Edited moments later: are you really using a shape file, because they don't support <Null> values....
Sorry about that, I had different variations trying to figure out what the problem is. Even when adding arcpy.MakeFeatureLayer_management it still deletes all the features.
TaxPar = r"C:\Temp\TaxParcels1.shp"
# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(TaxPar, "Par")
query = "DXF_TEXT LIKE 'Q%' or DXF_TEXT = ' ' or DXF_TEXT IS NULL"
arcpy.SelectLayerByAttribute_management("Par", "NEW_SELECTION", query)
result = arcpy.GetCount_management("Par").getOutput(0)
print ('{} has {} records'.format("Par", result[0]))
if int(arcpy.GetCount_management("Par").getOutput(0)) > 0:
arcpy.DeleteFeatures_management("Par")
I created a shapefile just to try what you are doing I get really weird results in a SQL select window. I bet I haven't created a shapefile in over 10 years, and this is one of the reasons why:
The file is a in filegeodata base, I exported out to a shape to test.
I restarted my pc and low and behold it worked.
If your data is in a FGDB why bother testing on a shapefile?
Because it wasn't working in a FGDB.