Select to view content in your preferred language

Delete Feature Class by specific name from Geodatabase using arcpy

666
8
10-21-2022 10:30 AM
CCWeedcontrol
Regular Contributor

I have a SDE feature data set with certain feature class I need to delete. I need to be able to delete them by name, because there are other feature class in there that I do not need to delete.

I have the following, it runs but it doesn't delete anything. I get no error either.

I am using this in a Calculate Value model.

Expression

FEMA1()

Code block

import arcpy, os 

def FEMA1():

    arcpy.env.workspace = r"C:/Users/***/AppData/Roaming/Esri/ArcGISPro/Favorites/***.sde/CCDS.CCDS.TempLyrs"
    cws = arcpy.env.workspace

    fc_Delete = ["CCDS.CCDS.FEMA_FLOOD_ZONES_Temp","CCDS.CCDS.FLOODWAY_Temp","CCDS.CCDS.LOMAs_Temp","CCDS.CCDS.LOMRs_Temp","CCDS.CCDS.FEMA_Cross_Sections_Temp","CCDS.CCDS.FEMA_Base_Flood_Elevations_Temp", "CCDS.CCDS.FEMA_PANELS_Temp"]

    for fc in fc_Delete:
        fc_path = os.path.join(cws, fc)
        if arcpy.Exists(fc_path):
            arcpy.Delete_management(fc_path)
        else:
            return ("Feature classes do not exist")

 

0 Kudos
8 Replies
drWood
by
New Contributor III

I'm not entirely familiar with this arcpy command, but it looks like the error is that there is no reference to the Return value of the arcpy.Exists line of your code. It should be formatted so that if the return value is the boolean value for True that the next line of code runs or skips if it is not true.

-Derek Wood

0 Kudos
BlakeTerhune
MVP Regular Contributor

There must be some formatting issue with the path of the feature class. Try running this as a standalone script and tinker with the path until you figure out the problem.

import arcpy
import os

arcpy.env.workspace = r"C:/Users/***/AppData/Roaming/Esri/ArcGISPro/Favorites/***.sde/CCDS.CCDS.TempLyrs"
cws = arcpy.env.workspace
fc_known_to_exist = os.path.join(cws, "CCDS.CCDS.FEMA_FLOOD_ZONES_Temp")
if arcpy.Exists(fc_known_to_exist):
    print(f"The feature class exists: {fc_known_to_exist}")
else:
    print(f"{fc_known_to_exist} does NOT exist.")
0 Kudos
dgiersz_cuyahoga
Occasional Contributor

Adding on to this: is the "CCDS.CCDS.TempLyrs" in your workspace string a Dataset?

First thing I would try is to remove it, as I don't think it's needed.

#CLE #sloth
CCWeedcontrol
Regular Contributor

Code does run fine as standalone script.

0 Kudos
RhettZufelt
MVP Notable Contributor

Expression/Code Block.  Looks like maybe you are trying to run this in the Calculate Field tool?

Are you trying to delete feature class(es) or update the value in an attribute table or both?

R_

CCWeedcontrol
Regular Contributor

Trying to delete feature classes by name if they exist.

0 Kudos
by Anonymous User
Not applicable

@CCWeedcontrol wrote:

I am using this in a Calculate Value model.


This needs to be in a script tool or somewhere else other than in a Calculate Field operation.

Calculate Field only calculates the values of a field for a feature class, feature layer, or raster.  

0 Kudos
CCWeedcontrol
Regular Contributor

That is what I figured. So if I had to do this in model builder how would I go about it?

I've tried creating a model using a "If data Exists" and Iterate Feature class but was unsuccessful.

0 Kudos