ClearWorkspaceCache: For Your "This Should Work. Why Isn't It Working?" Needs

218
3
07-29-2025 01:31 PM
JeffreyThompson2
MVP Frequent Contributor
3 3 218

I have been struggling for several days trying to convert a legacy Python 2/ArcMap based script to run in the Python 3/ArcPro environment. It is a multi-step, data management script that involves clearing out the old data and putting in the new data. The script kept failing before a call to TruncateTable with "ERROR 000187: Only supports Geodatabase tables and feature classes." and/or at Compact with "ERROR 000535: Workspace does not exist." This didn't make sense since the data being feed to TruncateTable was a feature class and the Workspace being compacted did exist. And this script works just fine in the ArcMap runtime.

We figured pretty early on that it was probably some sort of data locking issue or possibly something to do with the file path names being interpreted differently in the different flavors of Python. We tried writing our file paths differently, explicitly starting and stopping an edit session, and a bunch of other stuff. None of that worked.

Then, I found a StackExchange answer that mentioned ClearWorkspaceCache. I threw arcpy.management.ClearWorkspaceCache() on the line before the TruncateTable and Compact calls and the script just worked. So the data was in fact being locked by the caching.

Moral of the story: If you have a script that doesn't work and you don't know why and it's giving you errors that don't make sense, try ClearWorkspaceCache().

3 Comments
Contributors
About the Author
A frequently confused rock-hound that writes ugly, but usually functional code.