There are several previous posts about this but I can't seem to figure this out.
I have a versioned feature class that I am trying to write a python script for that will remove all the data from the table using either TruncateTable or DeleteRows. I have tried to start an edit session prior to this function but I keep getting the "Cannot Update Outside of an Edit Session" error. Code below. Any ideas what I'm doing wrong?
Thanks,
Josh
import arcpy, os
sde = r"C:\Users\User\AppData\Roaming\Esri\ArcGISPro\Favorites\sdeConnection.sde\FeatureDataset"
fc = r"C:\Users\User\AppData\Roaming\Esri\ArcGISPro\Favorites\sdeConnection.sde\FeatureDataset\FeatureClass"
workspace = os.path.dirname(sde)
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True)
edit.startOperation()
arcpy.management.DeleteRows(fc)
edit.stopOperation()
edit.stopEditing(True)
Can the Workspace be a feature class or feature dataset?
I've always set the workspace as a .gdb or .sde
I'm curious if it works if you set the workspace as:
r"C:\Users\User\AppData\Roaming\Esri\ArcGISPro\Favorites\sdeConnection.sde
Thanks for the reply @Tom_Laue !
The os.path.dirname(sde) takes off the feature dataset from the path and leaves only "C:\Users\User\AppData\Roaming\Esri\ArcGISPro\Favorites\sdeConnection.sde" - but not sure if this is the best way to do this.
I tried setting workspace as the sde connection, but I got the same results.
@RhettZufelt Yes, it is versioned, moving edits to base.
Good to know about truncate table - thanks!
This is working for me on a SDE featureclass, versioned, moving edits to base.
import arcpy, os
sde = r"C:\Users\User\AppData\Roaming\Esri\ArcGISPro\Favorites\sdeConnection.sde"
fc = r'C:\Users\User\AppData\Roaming\Esri\ArcGISPro\Favorites\sdeConnection.sde\FeatureDataset\FeatureClass'
edit = arcpy.da.Editor(sde)
edit.startEditing(False, True)
edit.startOperation()
arcpy.management.DeleteRows(fc)
edit.stopOperation()
edit.stopEditing(True)
Not sure, but suspect it has something to do with the workspace path.
You say that os.path.dirname(sde) is reporting: "C:\Users\User\AppData\Roaming\Esri\ArcGISPro\Favorites\sdeConnection.sde".
Is that when you print(workspace), or just enter workspace at the prompt? If this is the actual path being sent to the editor (just typing workspace at the prompt), then it will have issues. The editor is expecting "C:\\Users\\User\\AppData\\Roaming\\Esri\\ArcGISPro\\Favorites\\sdeConnection.sde".
So, if the above code still doesn't work, I'd try hardcoding the workspace to see if it is related to that.
edit = arcpy.da.Editor(r"C:\Users\User\AppData\Roaming\Esri\ArcGISPro\Favorites\sdeConnection.sde")
Should at least help narrow down the issue.
R_
Thanks @RhettZufelt
When I put "C:\\Users\\User\\AppData\\Roaming\\Esri\\ArcGISPro\\Favorites\\sdeConnection.sde" in the sde variable, it worked! It seems for some reason it doesn't like the raw string syntax for the sde path. Any idea why that could be?
See below for what worked for me, noting the changes at the sde variable.
import arcpy, os
sde = "C:\\Users\\User\\AppData\\Roaming\\Esri\\ArcGISPro\\Favorites\\sdeConnection.sde"
fc = r"C:\Users\User\AppData\Roaming\Esri\ArcGISPro\Favorites\sdeConnection.sde\FeatureDataset\FeatureClass"
edit = arcpy.da.Editor(sde)
edit.startEditing(False, True)
edit.startOperation()
arcpy.management.DeleteRows(fc)
edit.stopOperation()
edit.stopEditing(True)
My testing had no issue with the raw string path if I set it directly in the arcpy.da.Editor(sde) instead of using workspace variable, but after the os.path.dirname(sde) it was having issues.
Not sure, I think it was returning the path as a string, not raw string so wasn't valid as input.
R_
Also, if it is a large table with no attachments (since truncate won't truncated associated attachments tables) it may be faster to unregister as versioned, truncate, then re-register as versioned.
R_