Fast way to delete row from table

924
3
03-06-2012 02:49 PM
zhengniu1
New Contributor
hi, all

I am trying to delete row from file geodatabase table using following code:
import arcpy
import math

arcpy.env.workspace = r'C:\TempArcGISCalculate\Scratch.gdb'
arcpy.env.overwriteOutput = True

intable="Section0011"

Statstbl="C:/TempArcGISCalculate/Scratch.gdb/S11FirstStats"
Query = '"FREQUENCY"=1'

records = arcpy.SearchCursor(Statstbl,Query)
singlelst = []
for record in records:
    singlelst.append(record.getValue('Trace')) # it adds data who has single point only

del records
del record


pts = arcpy.UpdateCursor(intable)

for pt in pts:
    for i in singlelst:
        if pt.Trace == int(i):
            print "Deleting:", pt.OBJECTID, pt.Trace
            pts.deleteRow(pt)

del pts, pt            


In the table "Section0011", I have 534975 rows. When the script runs, it only remove about 1000 rows in couple hours, but has not finished yet. Is there any fast way to run deleteRow? please help

Thanks lot
Tags (2)
0 Kudos
3 Replies
MarcinGasior
Occasional Contributor III
In last part of the script try:
for pt in pts:
        if pt.Trace in singlelst:


instead of:
for pt in pts:
    for i in singlelst:
        if pt.Trace == int(i):


I simulated similar script situation with this code change and for feature class with over 550 000 rows, ~13 000 rows were deleted in less than 4 minutes.
0 Kudos
MathewCoyle
Frequent Contributor
Yes, I would implement Marcin's change. Your original script is running through every feature you want to delete for every row in your dataset. Uneconomical to say the least.
0 Kudos
zhengniu1
New Contributor
Marcin and Mathew, Thank you so much
0 Kudos