How to delete a network analysis dataset via a script?

364
3
Jump to solution
04-21-2021 08:52 AM
CassKalinski
New Contributor III

I am trying to delete the network analysis dataset as part of a workflow, but having no success. Is there an arcpy protocol that can be used to delete the dataset and feature classes? I have attempted to use the arcpy.Delete_management() call with no success.

Did see this in the Data Management - Delete documentation...
'Feature classes and tables participating in a network analysis dataset or a topology cannot be deleted'

But I can manually delete them from the GDB after I am done with them. But not with a script?

# General workflow...
na_lyr_name = "Service Area"
na_result = arcpy.na.MakeServiceAreaAnalysisLayer(routing_src, na_lyr_name, etc)
arcpy.na.AddLocations(na_lyr_name, etc)
arcpy.na.Solve(na_lyr_name, "SKIP", "TERMINATE", None, '')
na_lyr = na_result[0]

# Go create feature layers off the lines and polygon results of the network analysis...
# Then clean up the now unneeded NA dataset...

# First tried just deleting the dataset itself...
arcpy.Delete_management(na_lyr)
# or...
arcpy.Delete_management(na_lyr_name) 

# Then tried looping through the layers
for lyr in na_lyr.listLayers():
    layer_path = os.path.join(DATA_GDB, lyr.name)    
    temp = arcpy.Delete_management(layer_path)
    # debug...returns True and layer name
    print(temp, lyr.name)

 

No errors thrown. The 'temp' print statement in there shows the Delete reporting successful. But the dataset remains in the GDB when I examine the file. 

Context:

This is a workflow that will be repeated quarterly, maybe monthly. The process is pretty standard: build a network along roadways from fixed points to create a service area. Then buffers are then built off the service area and passed on to a web map and web app.

I could just have someone manually go in after every run to delete the NA layers but would prefer the script clean up after itself. 

0 Kudos
1 Solution

Accepted Solutions
CassKalinski
New Contributor III

Moving the code I had before to the front did not work, but that lead me to another solution. Running this at the start of the script works...

 

dataSets = arcpy.ListDatasets("ServiceArea*", "All")
if dataSets:
    arcpy.AddMessage('Deleting prior datasets...')
    for d in dataSets:
        print(d)
        arcpy.Delete_management(d)
    arcpy.AddMessage('\tPrior service area datasets deleted')
else:
    arcpy.AddMessage('No prior service area datasets found')

 

 

Network Analysis names all the datasets 'ServiceArea###', with that last piece being some kind of hash value. Can key off that to build the list to delete. Interestingly, using arcpy.ListDatasets("*", "Network") does not pick up the datasets.

Thank you Dan for the nudge in a working direction!

View solution in original post

3 Replies
DanPatterson
MVP Esteemed Contributor

Have you tried deleting what may exist from a previous run first at the start of your script?

In other words if it "exists" delete it, then proceed on from there. (move your delete management line to the top)


... sort of retired...
0 Kudos
CassKalinski
New Contributor III

Will give that a try...

0 Kudos
CassKalinski
New Contributor III

Moving the code I had before to the front did not work, but that lead me to another solution. Running this at the start of the script works...

 

dataSets = arcpy.ListDatasets("ServiceArea*", "All")
if dataSets:
    arcpy.AddMessage('Deleting prior datasets...')
    for d in dataSets:
        print(d)
        arcpy.Delete_management(d)
    arcpy.AddMessage('\tPrior service area datasets deleted')
else:
    arcpy.AddMessage('No prior service area datasets found')

 

 

Network Analysis names all the datasets 'ServiceArea###', with that last piece being some kind of hash value. Can key off that to build the list to delete. Interestingly, using arcpy.ListDatasets("*", "Network") does not pick up the datasets.

Thank you Dan for the nudge in a working direction!