1

I have a geodatabase and I would like to be able to delete layers in there easily using ArcPy. Also select which type of files.

Example: delete all file tables from the gdb or all layers.

Code:

env.workspace = r"path.gdb"
for i in arcpy.ListFeatureClasses():
    #how to delete?
  • 1
    Just something to consider, if you wanted to purge a geodatabase completly (i.e. remove all it's contents) it will be quicker to delete the geodatabase itself and create another. You would do that if you don't care about any domains or metadata. – Hornbydd Oct 25 '18 at 21:09
  • Be aware that geodatabases do not contain layers but they can contain feature classes that layers in a map or layer-file are using as their source. – PolyGeo Oct 25 '18 at 21:21
3
env.workspace = r"path.gdb"
fc_list = arcpy.ListFeatureClasses()
tables = arcpy.ListTables()
ds_list = arcpy.ListDatasets()

#feature classes
for fc in fc_list:
    arcpy.Delete_management(fc)

#tables
for table in tables:
    arcpy.Delete_management(table)

#data sets
for ds in ds_list:
    arcpy.Delete_management(ds)

There are many options to modify each of list so head to help links to get more info.

Help links:

feature clases - http://pro.arcgis.com/en/pro-app/arcpy/functions/listfeatureclasses.htm

tables - http://pro.arcgis.com/en/pro-app/arcpy/functions/listtables.htm

data sets - http://pro.arcgis.com/en/pro-app/arcpy/functions/listdatasets.htm

delete - http://pro.arcgis.com/en/pro-app/tool-reference/data-management/delete.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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