I have written this script that recurses through all feature classes in a given set of feature datasets and deletes all their features. It appears to do the job but it runs pretty slowly. Is there anything i'm doing wrong here or are there any obvious ways to speed things up?
I'm using DeleteFeatures_management to do the deed. DeleteRows_management also seems to work.
import sys
import os
import arcpy
from arcpy import env
import datetime
import getpass
try:
passwd = getpass.getpass("Enter the sde user password: ")
sdeConnectionFileDir = os.environ.get("TEMP")
databaseName = ""
fileName = "temp.sde"
# Delete any pre-existing SDE connection file.
fullPath = sdeConnectionFileDir + '\\' + fileName
if os.path.exists(fullPath):
os.remove(fullPath)
# Create temporary SDE connection file.
arcpy.CreateArcSDEConnectionFile_management (
sdeConnectionFileDir, fileName,
"sdeserver", "5151", "",
"DATABASE_AUTH", "my_sde_user", passwd,
"SAVE_USERNAME", "SDE.DEFAULT", "SAVE_VERSION"
)
env.workspace = fullPath
# ArcPy status codes.
returnCodes = {'WARN' : 0, 'INFO' : 1, 'ERROR' : 2}
featureDatasets = []
featureDatasets.extend(arcpy.ListDatasets("dataset1*"))
featureDatasets.extend(arcpy.ListDatasets("dataset2*"))
featureDatasets.extend(arcpy.ListDatasets("dataset3*"))
list = '[%s]' % ', '.join(map(str, featureDatasets))
response = raw_input("\n***** WARNING!!! ***** \nAll data will be deleted from all feature classess in the following datasets: \n\n" + list + "\n\n |--> Type DELETE to begin removal: ")
if response == "DELETE":
print "\nStarted: " + str(datetime.datetime.now()) + "\n"
for dataset in featureDatasets:
print "Processing dataset: " + dataset
for fc in arcpy.ListFeatureClasses("*", "ALL", dataset):
rowCount = int(arcpy.GetCount_management(fc).getOutput(0))
if rowCount > 0:
print " -- Processing feature class: " + str(fc) + " (" + str(rowCount) + " rows)"
#arcpy.DeleteRows_management(fc)
arcpy.DeleteFeatures_management(fc)
print "\nCompleted: " + str(datetime.datetime.now())
except Exception as e:
if arcpy:
arcpyErrors = arcpy.gp.getMessages(returnCodes['ERROR'])
if arcpyErrors:
sys.stderr.write(arcpyErrors + "\n")
sys.stderr.write(str(e) + "\n")
sys.exit(1)
EDIT --
I put some performance timers in the script and here is the data:
- Time to retrieve datasets: 0:00:01.254000
- Total Feature Classes: 1682
- Total Feature Classes with Data: 124
- Total Features Processed: 190222
- Total Run Time: 3 hours, 16 minutes
The breakdown:
Feature dataset --> list feature class calls:
* AVG 0:00:02
* MIN 0:00:01
* MAX 0:00:07
* COUNT 40
* TOTAL 0:01:08
Feature count calls (majority of the time):
* AVG 0:00:06
* MIN 0:00:01
* MAX 0:00:16
* COUNT 1682
* TOTAL 2:41:00
Feature deletion calls (reduced because only feature classes with rows are processed):
* AVG 0:00:17
* MIN 0:00:02
* MAX 0:03:22
* COUNT 124
* TOTAL 0:34:31