I am having an issue where I cannot delete a version because it is said to still be in use, but this error only arises when I use the script from an ArcGIS Tool in the Toolbox. When I run the script from an outside IDE, or even the same commands from the python window, it finishes without error.
The general process is:
1) create a new version "Child"
2) Loop of feature classes:
2.1) create feature layer
2.2) change to version "Child"
2.3) various queries etc
2.4) copy feature layer
2.5) change to version Default
3) run arcpy.ClearWorkspaceCache_management() [updated]
4) delete version "Child" --- arcpy.DeleteVersion_management(sde_database, "username".Child)
The error returned by the tool dialogue is:
Executing: DeleteVersion "/path/connectionfile.sde" "username".Child
Start Time: Thu Feb 19 13:12:24 2015
ERROR 999999: Error executing function.
The version is currently in use. ["username".Child]
Failed to execute (DeleteVersion).
At this point I will go to the Version Manager to delete the Child version manually and I get the following error:
Could not delete version "username".Child.
[An event was unable to invoke any of the subscribers (Exception from
HResult: 0x80040201)]
If I close and reopen ArcMap I can then delete the version. Does anyone have an idea what might be causing this and how to resolve it?
I'm just guessing here, but try running the Clear Workspace Cache Data Management tool before you delete the version.
Thanks for the reply. I'm trying to not post all of the code just so I don't have to go through and edit everything to remove various info bits-and-pieces, but I am actually running arcpy.ClearWorkspaceCache_management() just prior to attempting to delete the Child version. Thanks for the suggestion.
It doesn't sound like you are doing any edits (step 2.3 and 2.4), but are you starting an edit session in there anywhere?
ArcGIS Help 10.2 - Editor (arcpy.da)
If yes, are you explicityly stopping the edit session?
If no, why do you need to create a version for read-only operations?
EDIT:
Just to verify your code works, try a simplified version of your script that just creates the version and then deletes it. Then scale it up to create the version, switch to it, switch back to Default, then delete the version. The success or failure of these actions might help troubleshoot the problem.
Sorry for the late reply. I thought I had this setup to email me if someone responded.
I am not doing any editing to the version I've created. I create it because I want to connect to a version that I am not the owner of, and I've found that I'm not allowed to do that from a script (although I can connect to it from within ArcMap). So I started creating a new version which I am the owner of and can connect to from the script with the version I want the data from being the parent.
I don't explicitly start an edit session. I suppose one of the steps I take starts one (?). I make edits to a copy of the data that I've saved to a local geodatabase. I imagine then that an edit session starts when I perform those changes (adding fields and populating those fields). I tried to explicitly close the edit session and it did not work.
I began stepping through the code like you recommended and found that the step where I am no longer able to delete the version I created is the first selection i run on the featurelayer, which is a spatial selection. I apply the spatial selection, then copy out the selected features, then change versions back to default, and then try to delete the created version but cannot because it is "still in use".
I'm still working away at why this is, but this is the update in case you have any idea what may be the issue. Thanks a lot for your help.
Try reconciling the version before you delete it. However, I still don't understand the requirement to do this work in a version if you're only selecting features and exporting them. If you are not editing geometry or attributes, I don't think you need a version.
Well, I'm not sure either. I want to connect to someone else's version and I get an error that I don't have access when I try to do this from a stand-alone script. I suppose because my username is not that of the owner. Creating the version is just the work around I found that works.\
I'll try reconciling. I also tried deleting the featurelayer that I was working from. I felt pretty good about that solving the issue, but it didn't.
Oh, so the version you make is created under another version that has changes you want to see? Were you using arcpy.ChangeVersion_management() to change versions? Can the changes in the parent version be posted up so you can just do the script work on Default?
Yes to the first question. Nope to the second. Other people make changes and post them to the version I want to target, but I'm not the owner of it so I can't target it directly. So I'm making version of that version with other peoples updates. None of those changes have been posted to default yet, won't be for days.
The version has to be created as Public or Protected if you are not the owner and need to view it. If the version is Private, you have to be the owner.
ArcGIS Help 10.2 - Creating versions and setting permissions