Delete Feature Class from Geodatabase using arcpy not working

4395
4
Jump to solution
09-02-2020 11:41 PM
PaulaMorrissey
New Contributor II

I have looked at other the scripts to delete feature classes in Geodatabases but all I can do is remove the feature classes from the Contents window but they are still in the Geodatabase in Catalog. I'm not sure that I am using the correct function Delete_management

import arcpy

arcpy.env.workspace = r"C:\Data\Temp.gdb"

fc_Delete = ["fcOut1a","fc_Out2a","fc_Out3a"]

for fc in fc_Delete:
if arcpy.Exists(fc):
arcpy.Delete_management(fc,"")

0 Kudos
2 Solutions

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

This is an ArcPy question, so I am sharing with Python‌.  Questions here are more for ArcGIS API for Python.

Personally, I never rely on the workspace to assume path names because doing so inevitably causes issues like this one.  If you are running this code in the interactive Python window, then what you are removing isn't the feature class but the layer that has the same name as the feature class.  Even though you set the workspace, the Delete tool gives preference to layers before data sets.  If you want to delete the feature class, pass the full path of the FC to the tool.

View solution in original post

PaulaMorrissey
New Contributor II

And that worked too.Thanks

View solution in original post

0 Kudos
4 Replies
JoshuaBixby
MVP Esteemed Contributor

This is an ArcPy question, so I am sharing with Python‌.  Questions here are more for ArcGIS API for Python.

Personally, I never rely on the workspace to assume path names because doing so inevitably causes issues like this one.  If you are running this code in the interactive Python window, then what you are removing isn't the feature class but the layer that has the same name as the feature class.  Even though you set the workspace, the Delete tool gives preference to layers before data sets.  If you want to delete the feature class, pass the full path of the FC to the tool.

PaulaMorrissey
New Contributor II

I added the full path and that worked, Thanks

0 Kudos
LanceKirby2
Occasional Contributor

You could reassign your workspace to a new variable and do something like

import arcpy, os 

arcpy.env.workspace = r"C:\Data\Temp.gdb"
cws = arcpy.env.workspace

fc_Delete = ["fcOut1a","fc_Out2a","fc_Out3a"]

for fc in fc_Delete:

  fc_path = os.path.join(cws, fc)
  if arcpy.Exists(fc_path):
    arcpy.Delete_management(fc_path)

PaulaMorrissey
New Contributor II

And that worked too.Thanks

0 Kudos