1

I want to use the shutil.rmtree() to remove files from a scrap geodatabase. Before adding it to a stand alone script I tried it out in an interactive session and got two different results. The first time the code seemed to work fine. I ran it and opened arccatalog and all the files had been removed from the gdb:

clearfile = r"C:\RS_Data\Non_RS_MXDs\CO_002_Scrap.gdb"
shuntil.rmtree(clearfile)

I then tested it on the next gdb and when I opened arccatalog this time the actual gdb had been deleted and the gdb from the previous excution is displayed in the catalog tree as a folder and not a gdb. I'm pretty sure I used the same syntax but here it is just in case:

clearfile = r"C:\RS_Data\Non_RS_MXDs\CO_059_Scrap.gdb"
shutil.rmtree(clearfile)

I don't want the gdb deleted, just the files within as each time I run the script the last bit of code builds a new featureclass from merging other featureclasses in the scrap geodatabase created at the beginning of the script. Therefore the gdb needs to be empty at the beginning of the script or I will get unwanted featureclasses merged into the final output. Also, I don't want to create a new scrap gdb everytime I run the script.

  • A file geodatabase encompasses dozens of internal files. If you delete them all you no longer have a file geodatabase (and might as will delete the top-level directory as well). Be sure you distinguish correctly between "files" and "tables" (it takes several, sometimes dozens, of files to implement a single table). – Vince Dec 3 '15 at 19:06
  • 1
    There are files within the .gdb folder that ArcGIS/Catalog use even if the GDB is 'empty.' Without those files, it has no way of telling it apart from a normal folder. It's likely that ArcGIS had a lock on those files when you ran it interactively, so they didn't get deleted, but that's not happening when you run it from a stand alone script. – Evil Genius Dec 3 '15 at 19:06
  • rmtree is supposed to delete the entire folder, per the documentation. If it didn't on the "first time," that could only be some kind of error. – jpmc26 Dec 3 '15 at 22:16
7

I would strongly recommend you don't do what your are doing. A File GeoDatabase may appear to be a folder with a bunch of weird files in it, outside ArcMap, but those files and their meaning are controlled by ArcMap. So you going in and blitzing them is essentially corrupting the GeoDatabase. You have two options for generating robust code:

  1. Delete the entire geodatabase so what looks like the folder r"C:\RS_Data\Non_RS_MXDs\CO_002_Scrap.gdb" then reconstruct the GeoDatabase from scratch.
  2. Remove the datasets from within the GeoDatabase using the GIS. In this case you would use arcpy and call the Delete GeoProcessing tool. This way maintains the integrity of the GeoDatabase.
  • 2
    I'd add variant 1b: Delete the folder and extract a zipfile of the original empty geodatabase tree in its place. But there's also issues with lock files and concurrent use to manage. ArcGIS Server creates a new scratch folder with a new scratch geodatabase for each job, just to make sure there's sufficient independence. – Vince Dec 3 '15 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.