Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I can't get my ArcPy script to delete a terrain. If it exists, it should be deleted. However it throws an

ERROR 000317: {path} cannot be deleted

My script is a lot longer than this, but this is what it boils down to. fds is a feature dataset in a file geodatabase. The feature dataset contains a terrain with 2 associated feature classes:

  • a points feature class used as masspoints
  • a polygon feature class used as boundary/softclip

Here's my code:

import os
import arcpy

fds = r'D:\Dumfries & Borders.gdb\DEM'

arcpy.env.workspace = fds
l = arcpy.ListDatasets()
for d in l:
    if arcpy.Exists(os.path.join(fds, d)):
        arcpy.Delete_management(os.path.join(fds, d))

Earlier on I tried to have the script delete the whole feature dataset at once. Didn't work either. I then decided to delete the datasets in it one by one. The ListDatasets() lists only one: the terrain, which sparks the error mentioned above.

It is possible to delete the terrain or the feature dataset by hand in ArcCatalog, or using the Python window in ArcCatalog. However, even after closing all other programs (to preclude any locks from obstructing) except my Python interpreter (PyCharm) it still throws the same error.

So, any ideas? Will post this to the ESRI forums as well.

edit follow-up: removing the spaces and & from the gdb's name does not help, as ListDatasets() won't list anything now anymore. Also scriptwise trying to delete the feature dataset now throws an

ERROR 000732: Input Data Element: Dataset {path} does not exist or is not supported.

share|improve this question
    
Have you tried restarting your computer? Lock files can be pesky sometimes. Since you are setting the workspace equal to fds, you shouldn't have to use os.path.join(). –  Paul Aug 29 at 15:00
    
@Paul That was a nice idea, but it doesn't work. Still at the same position as per the follow-up. I tried using arcpy.Exists() to check if terrain or feature dataset exist: apparently they don't, although they really are in the gdb (as seen in arccatalog) and can be queried and viewed fine. –  Menno Aug 29 at 15:31
    
One observation: it is never a good idea to have paths with special characters (except for "_") such as Dumfries & Borders.gdb. Rather, remove the spaces and other special characters e.g. r'D:\Dumfries_and_Borders.gdb\DEM' –  Aaron Aug 29 at 17:51
1  
@Aaron I know, but the request is specifically to call the geodatabases exactly after the districts... However I will have to pursuade my client to change it. –  Menno Sep 1 at 8:04

1 Answer 1

up vote 6 down vote accepted

I was able to replicate your problem, but found a solution. You have to remove the datasets that are used by Terrain dataset first before you can delete it. A bit weird but that seems the way it must work...

So the following code completely removed the FeatureDataset containing the Terrain datasets and the FeatureClass used to construct it. The order that the things are deleted is important. This ran from Pyscripter when no ArcGIS application was open.

import os
import arcpy

# Inputs
fds = r'C:\Scratch\fGDB_Scratch.gdb\test'
terrainDS = r'C:\Scratch\fGDB_Scratch.gdb\test\test_Terrain'
fc = r'C:\Scratch\fGDB_Scratch.gdb\test\ppnts'

try:
    arcpy.CheckOutExtension("3D")

    print "Deleting points from terrain"
    arcpy.DeleteTerrainPoints_3d(terrainDS,"ppnts","ppnts")

    print "Deleting Terrain Dataset"
    arcpy.Delete_management(terrainDS)

    print "Deleting featureclass"
    arcpy.Delete_management(fc)

    print "Deleting FeatureDataset"
    arcpy.Delete_management(fds)

    print "Finished!"
except Exception as e:
    print e
finally:
    arcpy.CheckInExtension("3D")

You have also used a polygon dataset, so may need to run the Remove Feature Class From Terrain as well, but I have not tested this.

share|improve this answer
1  
Good answer. One point, should except e: be except Exception as e:? –  Aaron Aug 29 at 17:47
    
Thank you for spotting the syntax error, now corrected! –  Hornbydd Aug 29 at 20:16
    
Thanks, this worked flawlessly! –  Menno Sep 1 at 11:11

Your Answer

 
discard

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.