0

I've written a script in a python toolbox. That script creates a folder in the user defined workspace for all the output files that are created during the script process. When the script is done, I don't need these files anymore because I've saved my results in an extra folder. I want to delete this temporary folder and all of its files and wanted to use the delete_management tool like this:

# Set Workspace
    arcpy.env.workspace = parameters[2].valueAsText

    # Set Temporary Output
    arcpy.CreateFolder_management(arcpy.env.workspace, "Temp")

    # Delete temporary Files
    arcpy.Delete_management("Temp")  

But that clearly doesn't work and it is not even giving me an error message. The delete line just has no effect. What am i doing wrong here?

edit: I'm using ArcGIS 10.1 and the parameters[2] string is the users input.

  • Are you verifying the workspace parameter to make sure it's a valid input? I'd guess Temp wouldn't be created at all if not, but doesn't hurt to check. Try hardcoding the input, see if that works first. – recurvata Sep 16 '14 at 18:51
  • If you have a file with a .loc file attached in your folder, you will not be able to delete the folder. It is worth a check. – Aaron Sep 16 '14 at 19:28
  • 1
    Delete does not use any environments. It is not aware of your 'workspace', thus passing it in the folder name wont work. You need to fully reference the path like the answers below indicate. resources.arcgis.com/en/help/main/10.2/index.html#/Delete/… – KHibma Sep 16 '14 at 22:58
4

The problem is that the Delete management task doesn't know what you are trying to delete. You either need to give it the path the to folder or create a variable to hold the path and pass that to delete management.

Try something like this

arcpy.env.workspace = parameters[2].valueAsText
tempfolder = arcpy.CreateFolder_management(arcpy.env.workspace, "Temp")

# Set Temporary Output
#arcpy.CreateFolder_management(arcpy.env.workspace, "Temp")

# Delete temporary Files
arcpy.Delete_management(tempfolder)
  • Thanks for your answer, but this doesn´t seem to change anything. I also tried this: arcpy.Delete_management(tempfolder, "Folder") but its still not working. I´m quite surprised, can´t be that hard to delete a simple Folder. =) Any more ideas? – user37088 Sep 16 '14 at 18:20
  • try using: shutil.rmtree(tempfolder) – detroit_hc Sep 16 '14 at 18:54
1

I might be wrong here, but if you're setting your env.workspace, and creating and deleting from there, I don't think you need to include them in CreateFolder or Delete_Management. That's kind of the point in setting the workspace.

CreateFolder may require it based on ArcHelp, although that seems counterintuitive. Delete should not.

Of course, you could use the python os module as well.

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.