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.

  • 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, 2014 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, 2014 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, 2014 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, 2014 at 8:04

1 Answer 1


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'


    print "Deleting points from terrain"

    print "Deleting Terrain Dataset"

    print "Deleting featureclass"

    print "Deleting FeatureDataset"

    print "Finished!"
except Exception as e:
    print e

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.

  • 1
    Good answer. One point, should except e: be except Exception as e:?
    – Aaron
    Aug 29, 2014 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.