Delete or ovewriting existing data in model builder

14341
23
Jump to solution
02-05-2015 10:20 AM
DanielErklauer
New Contributor III

I have a model that converts shape files to a feature dataset in an existing database.  I would like to either overwrite the existing feature classes in the db or delete them and perform the conversion.  Which method is the most efficient and how do I do it?

 

v10.2

1 Solution

Accepted Solutions
MicahBabinski
Occasional Contributor III

Hi there!

First off, let me just say that I feel your pain. This was (just) one of the limitations of ModelBuilder that eventually led me to embrace Python/arcpy as my solution of choice. If you're willing to go that route, you can export to a python script by editing the model and going to the Model menu > Export > to Python script. (Edit: seems like you're already aware of how to do this.)

From there, you have a couple of options. Option 1 is to set the overwriteOutput environment on your script by typing:

arcpy.env.overwriteOutput = True

at the beginning, but after importing the arcpy module.


If this fails to overwrite, you may employ a test for the existence of the feature class that you want to overwrite and if it does exist, delete it before running your conversion tool of choice. It would likely look something similar to:

# Iterate through your shapfiles with a for loop
for shp in shapefiles_to_convert:
     # Get the input to the conversion tool
     in_shp = shp
     # Get the feature dataset where your tool will output
     output_featureDataset = r"path_to_feature_dataset"
     # Get the base name of your output feature class
     output_name = arcpy.Describe(shp).baseName
     # Test to see if the output feature class already exists
     if arcpy.Exists(output_featureDataset + "\\" + output_name):
          # Delete it if it does
          arcpy.Delete_management(output_featureDataset + "\\" + output_name)
     # Run the conversion tool
     arcpy.FeatureClassToFeatureClass_conversion(shp, output_featureDataset, output_name)

I hope you find a solution to your problem!

Warm Regards,

Micah Babinski

View solution in original post

0 Kudos
23 Replies
LizaGrits
New Contributor II

When I convert shapefile to feature class in the geodatabase, I use Feaure Class to Feature Class tool and it does overwrite by default. Did you try and it didn't overwrite?

Lisa

0 Kudos
DanielErklauer
New Contributor III

Liza that is not a batch function unfortunately.

0 Kudos
LizaGrits
New Contributor II

Oh, I see. If you use FeatureClass to Geodatabase (multiple) it doesn't overwrite.

I would use Feaure Class to Feature Class tool and iterators to convert all the shapefiles.

LizaGrits
New Contributor II

Like this:

Capture.JPG

0 Kudos
DanielErklauer
New Contributor III

that's what I have...my folder GIS setting the env is recursive because the shapefiles are in directories withinExport Graphic.jpg the folder GIS.  When I run the model the second time it takes aproximatly1 second

0 Kudos
DanielErklauer
New Contributor III

I get the warning error 000258 name already used when I attempt to run the model again to check that it works

0 Kudos
LizaGrits
New Contributor II

What tool are you using? Is it just warning or the model doesn't run after this error? Do you want to show your model here?

0 Kudos
LizaGrits
New Contributor II

Could you try to save to GDB outside of Feature Dataset?

0 Kudos
DanielErklauer
New Contributor III

when I do that I get this

000258: Output S:\Shared\1 Survey Operations\Data Management\Databases\DB\HarkandDB_Nad27.gdb\protrac already exists

0 Kudos