Select to view content in your preferred language

Deleting features, tables, datasets from database not working correctly

1112
9
01-19-2023 09:48 AM
CCWeedcontrol
Frequent Contributor

I am trying to delete all feature classes, tables and datasets from a file database and the issue is sometimes it works and sometimes it doesn't. I trying the following but none of them works consistently. My thought is that, os.path.exists would be more reliable.

 

import arcpy, os
def deletePI():   
    try:
        arcpy.env.workspace = "C:/Temp/DashboardPI.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)

    except:
        pass

 

import arcpy, os

def deletePI():    
    try:
        arcpy.env.workspace = "C:/Temp/DashboardPI.gdb"
        
        if os.path.exists("C:/Temp/BoardPI.gdb/PI_Taxp") == True:
            arcpy.Delete_management(r"C:/Temp/boardPI.gdb/PI_Taxp")

 

0 Kudos
9 Replies
DanPatterson
MVP Esteemed Contributor

In windows explorer, examine the "C:/Temp/BoardPI.gdb" and you won't find a file called "PI_Taxp" since featureclass naming is handled by Pro


... sort of retired...
0 Kudos
RhettZufelt
MVP Notable Contributor

So, are you still seeing datasets in there with Pro after the delete?  Windows explorer will show a bunch of files in an "empty" FGDB, so can't tell using that if it worked or not.

And, are you trying to just preserve domains or something?

If not, would be quicker to just delete the FGDB and create a new one with the same name.

R_

0 Kudos
CCWeedcontrol
Frequent Contributor

I am trying to using this in a Calculate Value tool.

I am still seeing the datasets in there with Pro and I do see a bunch of files in the FGDB through Windows explorer.

The odd thing that I noticed is if I select the Calculate Value Tool in the Model Builder View window, right click and select run, its runs fine, everything gets deleted. If I right click on the Scheduled Model with this Calculate Value Tool in the Scheduled Tools window and selecting run, I get a warning that, "PI_Taxp" already exists in the C:/Temp/BoardPI.gdb". So it's not being deleted.

So it appears that if it's scheduled in a Pro project, it doesn't run...?

 

CCWeedcontrol_0-1674160912392.png

CCWeedcontrol_1-1674160925645.png

 

0 Kudos
RhettZufelt
MVP Notable Contributor

Could it be that the code you supplied is deleting it from C:/Temp/DashboardPI.gdb, but the message says it still exists in the C:/Temp/BoardPI.gdb

R_

0 Kudos
RhettZufelt
MVP Notable Contributor

Don't see the Calculate Value tool, that must be something custom?  Not sure what is going on there.

Didn't say why you need to delete everything from FGDB, is there a reason you can't just clobber it, then feed the new, empty FGDB to the rest of the tools?  Much faster/cleaner method to "empty" a FGDB.

 

RhettZufelt_0-1674161740167.png

 

R_

0 Kudos
CCWeedcontrol
Frequent Contributor

I guess there is not in this particular situation, I could delete the geodatabase with the delete tool and then create a new one.

I would still like to know why it's not delete with the Calculate Value Tool, if I run it through the task scheduler in Pro.

0 Kudos
RhettZufelt
MVP Notable Contributor

Without knowing what is in the calculate value tool (which seems to suggest it calculates a value, not deletes stuff) couldn't really speculate, as long as you got the correct FGDB's (since you mix the two in previous posts).

One thought, though not sure how it works in Model builder, vs straight python) if you are using something that only works on feature layers (a lot of the GP tools) and not directly on featureclasses, if you set it up in pro and select the data from the drop down menu, it is automatically loading a feature layer in the tool, not a feature class.  So, if run outside of that "open" Pro project, these types of tools will fail (Not sure if tasks scheduled through Pro actually open the document, or just runs in python outside of Pro).

This is one of the main reasons I've run into where it will run in Pro/Map, but not stand alone.

You could test if this is what is going on my using the Make Feature Layer tool on each dataset, then use the resultant feature layer in the tool, rather than the FC itself.

R_

CCWeedcontrol
Frequent Contributor

After setting up the model to use the delete tool and create file geodatabase tool, then scheduling it to run this morning, I get the warnings, the geodatabase already exists and so does the PI_Tax feature class. So nothing was deleted.

 

 

 

CCWeedcontrol_1-1674227894309.png

 

 

0 Kudos
RhettZufelt
MVP Notable Contributor

Don't know what to tell you.  If you add a precondition like I did in the example above so that the delete has to succeed before the create, then there is no way it could still exist unless you are working on different datasets or maybe some lock files are in place (though, shouldn't succeed in the delete if that is the case).

What happens if you make a new model with the delete tool, then create tool?  If that works, there is something else going on in the model/code somewhere.

Also, sometimes it helps to export the model to a python file as it can make it easier to see/notice path mismatches and such.

R_

 

0 Kudos