Select to view content in your preferred language

Removing feature classes in a GDB based on a list of feature classes in a different GDB

699
2
Jump to solution
10-06-2023 01:57 PM
GrantSmith122
Occasional Contributor

Hello, I need some help creating some code that will help prep a GDB for the importation of new data. I have two file geodatabases set up (staging and master), and I need to delete files from the master GDB before uploading new versions from the staging GDB. For example:

Master GDB:

  • bridge
  • road
  • building
  • lake

Staging GDB:

  • bridge
  • building

arcpy.env.workspace = staging_gdb
staging_fc = arcpy.ListFeatureClasses()
arcpy.AddMessage("adding " + str(staging_fc) + " feature classes to master data")

for fc in staging_fc:
arcpy.conversion.FeatureClassToGeodatabase(fc, master_gdb)
arcpy.AddMessage("copied " + str(fc) + " to master gdb")

Using the code I have now, when the 'bridge' and 'building' staging feature classes get added to the master, they have a '_1' added to the suffix since there are already existing feature classes under the same name. Is there a way to delete feature classes in the master GDB based off the list I create with arcpy.ListFeatureClasses()? I don't believe I can use arcpy.env.overwriteOutput, since the workspace has to be set to the staging GDB in order for the list to be captured. 

 

1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

(Definitely test this before you commit to it; I did not test it.)

A dictionary might be better, but this should work?

Basically, list all feature classes in the staging gdb, then compare that list to the list of feature classes in the master gdb and delete the ones in the master gdb with the same name as one in the staging gdb.

Feature datasets are considered their own workspace by arcpy. Despite this, feature classes in FDs are still beholden to the "no duplicate names" rule, so you have to check specially for them.

 

import arcpy
import os

# Grab free-floating feature classes in staging gdb
arcpy.env.workspace = stagingGDB
killList = [os.path.basename(fc) for fc in arcpy.ListFeatureClasses()]

# Grab feature classes in feature datasets in staging gdb
for fd in arcpy.ListDatasets("*", "Feature"):
    arcpy.env.workspace = os.path.join(stagingGDB,fd)
    for fc in arcpy.ListFeatureClasses():
        killList.append(fc)

# Check masterGDB for feature classes with same name as something in 
#   stagingGDB, then delete.
arcpy.env.workspace = masterGDB
for fc in arcpy.ListFeatureClasses():
    if os.path.basename(fc) in killList:
        arcpy.management.Delete(fc)
        
# Check in masterGDB feature datasets.
or fd in arcpy.ListDatasets("*", "Feature"):
    arcpy.env.workspace = os.path.join(masterGDB,fd)
    for fc in arcpy.ListFeatureClasses():
        if os.path.basename(fc) in killList:
            arcpy.management.Delete(fc)

 

 

View solution in original post

0 Kudos
2 Replies
AlfredBaldenweck
MVP Regular Contributor

(Definitely test this before you commit to it; I did not test it.)

A dictionary might be better, but this should work?

Basically, list all feature classes in the staging gdb, then compare that list to the list of feature classes in the master gdb and delete the ones in the master gdb with the same name as one in the staging gdb.

Feature datasets are considered their own workspace by arcpy. Despite this, feature classes in FDs are still beholden to the "no duplicate names" rule, so you have to check specially for them.

 

import arcpy
import os

# Grab free-floating feature classes in staging gdb
arcpy.env.workspace = stagingGDB
killList = [os.path.basename(fc) for fc in arcpy.ListFeatureClasses()]

# Grab feature classes in feature datasets in staging gdb
for fd in arcpy.ListDatasets("*", "Feature"):
    arcpy.env.workspace = os.path.join(stagingGDB,fd)
    for fc in arcpy.ListFeatureClasses():
        killList.append(fc)

# Check masterGDB for feature classes with same name as something in 
#   stagingGDB, then delete.
arcpy.env.workspace = masterGDB
for fc in arcpy.ListFeatureClasses():
    if os.path.basename(fc) in killList:
        arcpy.management.Delete(fc)
        
# Check in masterGDB feature datasets.
or fd in arcpy.ListDatasets("*", "Feature"):
    arcpy.env.workspace = os.path.join(masterGDB,fd)
    for fc in arcpy.ListFeatureClasses():
        if os.path.basename(fc) in killList:
            arcpy.management.Delete(fc)

 

 

0 Kudos
GrantSmith122
Occasional Contributor

Thank you so much Alfred! I cherrypicked the sections of code that applied to my situation and it looks to be working perfectly. What I was initially stuck on was switching back and worth between workspaces; I didn't know you could have one workspace, create a list from it, then switch to another workspace, create another list, and then finally compare the lists. Thanks again!

0 Kudos