<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Clearing Arc/Arcpy workspace locks in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118532#M9349</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;p.name would have to be a&amp;nbsp; string or list or some other iterable&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; p_name="pythonista"
&amp;gt;&amp;gt;&amp;gt; "python" in p_name
True&lt;/PRE&gt;&lt;P&gt;or&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; p_name = None
&amp;gt;&amp;gt;&amp;gt; "python" in p_name
Traceback (most recent call last):
&amp;nbsp; File "&amp;lt;string&amp;gt;", line 1, in &amp;lt;module&amp;gt;
TypeError: argument of type 'NoneType' is not iterable&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;so whatever p.name is, it isn't an iterable.&amp;nbsp; So to find out what it is try adding a print statement may be with a type statement in it&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; p_name = None
&amp;gt;&amp;gt;&amp;gt; type(p_name)
&amp;lt;type 'NoneType'&amp;gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 06:57:09 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2021-12-11T06:57:09Z</dc:date>
    <item>
      <title>Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118512#M9329</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Particularly with the release of ArcGIS 10.1 I have had a great deal of trouble with workspaces being locked within complex codes that are creating, reading from and writing to numerous feature classes and workspaces. These locks cause the code to fail intermittently at different places, although occasionally making it through without a problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have previously posted on this with a solution that sometimes helps reduce the problem, but is unable to eliminate it completely. That is using arcpy.Exists() and arcpy.Compact() in sequence upon the workspace. This combination of tools, when used in ArcGIS 10.0, seemed adequate, however it is no longer sufficient in 10.1 to prevent issues 100% of the time.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In response to this I have developed a new method for clearing tricky locks which, for my purposes, works 100% of the time. It is a small function that can be added to the start of any arcpy script, and it requires the psutil Python library, which can be acquired here: &lt;/SPAN&gt;&lt;A href="http://code.google.com/p/psutil/" rel="nofollow noopener noreferrer" target="_blank"&gt;http://code.google.com/p/psutil/&lt;/A&gt;&lt;SPAN&gt;. When using multiprocessing the code seems to work fine with the Parallel Python library, but will hang indefinitely if you are using the Multiprocessing library due to (I guess) the method in which child processes are spawned. However, the Parallel Python library has other issues, for example, I have had trouble with it when getting it to run checked out extensions such as Network Analyst.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The following codeblock shows the necessary imports, the function block and an example of the usage.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy

import os
import psutil


def clearWSLocks(inputWS):
 '''Attempts to clear ArcGIS/Arcpy locks on a workspace.

 Two methods:
&amp;nbsp; 1: if ANOTHER process (i.e. ArcCatalog) has the workspace open, that process is terminated
&amp;nbsp; 2: if THIS process has the workspace open, it attempts to clear locks using arcpy.Exists, arcpy.Compact and arcpy.Exists in sequence

 Notes:
&amp;nbsp; 1: does not work well with Python Multiprocessing
&amp;nbsp; 2: this will kill ArcMap or ArcCatalog if they are accessing the worspace, so SAVE YOUR WORK

 Required imports: os, psutil
 '''

 # get process ID for this process (treated differently)
 thisPID = os.getpid()

 # normalise path
 _inputWS = os.path.normpath(inputWS)

 # get list of currently running Arc/Python processes
 p_List = []
 ps = psutil.process_iter()
 for p in ps:
&amp;nbsp; if ('Arc' in p.name) or ('python' in p.name):
&amp;nbsp;&amp;nbsp; p_List.append(p.pid)

 # iterate through processes
 for pid in p_List:
&amp;nbsp; p = psutil.Process(pid)

&amp;nbsp; # if any have the workspace open
&amp;nbsp; if any(_inputWS in pth for pth in [fl.path for fl in p.get_open_files()]):
&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! Workspace open: %s' % _inputWS

&amp;nbsp;&amp;nbsp; # terminate if it is another process
&amp;nbsp;&amp;nbsp; if pid != thisPID:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! Terminating process: %s' % p.name
&amp;nbsp;&amp;nbsp;&amp;nbsp; p.terminate()
&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! This process has workspace open...'

 # if this process has workspace open, keep trying while it is open...
 while any(_inputWS in pth for pth in [fl.path for fl in psutil.Process(thisPID).get_open_files()]):
&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! Trying Exists, Compact, Exists to clear locks: %s' % all([arcpy.Exists(_inputWS), arcpy.Compact_management(_inputWS), arcpy.Exists(_inputWS)])

 return True


#######################################
## clearWSLocks usage example #########
#######################################

# define paths and names
workspace = 'C:\\Temp\\test.gdb'
tableName = 'testTable'

# create table
arcpy.CreateTable_management(workspace, tableName)

# usage of clearWSLocks
clearWSLocks(workspace)

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have also attached the code as a separate module (clearWSLocks.py), which can be used in the following fashion:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import clearWSLocks

# define paths and names
workspace = 'C:\\Temp\\test.gdb'
tableName = 'testTable'

# create table
arcpy.CreateTable_management(workspace, tableName)

# usage of clearWSLocks
clearWSLocks.clear(workspace)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:57:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118512#M9329</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-11T06:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118513#M9330</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi there Stacy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm mainly replying here so that I am subscribed to this thread, but also uncover possible alternatives (for me) as other comments come in as I am new to Python development.&amp;nbsp; Plus I will likely need to implement the code example you've provided!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've actually had a run-in with the locking issue, but was able to mitigate (actually, eliminate) the problem by reading/writing intermediate data to the IN_MEMORY space, then move that processed data to it's final location(s) in their relevent workspaces and repositories (PGDB and FDGB's).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am wondering, is there something in your application architecture that is limiting your ability to utilize this IN_MEMORY space to read/write intermediate data?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is the IN_MEMORY space inadequate for some reason (eg, the data is simply too large)?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The multiprocessing requirements do not allow for the use of the IN_MEMORY space?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyway -- great topic and useful code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Take Care,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;j&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Nov 2012 11:29:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118513#M9330</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2012-11-20T11:29:23Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118514#M9331</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have simple scripts that sometimes report a file lock and when I run them again immediately, just run normally.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It is very hard to make some processes give up the file locks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;At 10.1 it has been suggested in the technical sessions to use the 'with' construct which is guaranteed to close cursors instead of relying on garbage collection after a del cursor.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any other hints and experiences?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 22 Nov 2012 08:35:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118514#M9331</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2012-11-22T08:35:11Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118515#M9332</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Kim - for a while I required my code to run in both 10.0 and 10.1 at the same time, so I couldn't use the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;with&lt;/SPAN&gt;&lt;SPAN&gt; statement. However, I still had issues with locks after creating feature classes and adding fields, etc., which do not have a &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;with &lt;/SPAN&gt;&lt;SPAN&gt;statement...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;They really should implement something to let the program definitely close the file (like a&lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt; .close()&lt;/SPAN&gt;&lt;SPAN&gt; function), such as discussed by Sean Gillies in his excellent blog &lt;/SPAN&gt;&lt;A href="http://sgillies.net/blog/1067/get-with-it/"&gt;here&lt;/A&gt;&lt;SPAN&gt;. I guess the issue occurs because files are just represented by paths, not file objects as they are in GDAL/OGR.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 25 Nov 2012 18:46:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118515#M9332</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2012-11-25T18:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118516#M9333</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks James,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Is the IN_MEMORY space inadequate for some reason (eg, the data is simply too large)?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have seen a few people talk about IN_MEMORY, but I have never seen any help files or code examples using it, so I have never tried it out! Sounds like something I should look into!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Stacy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 25 Nov 2012 18:48:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118516#M9333</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2012-11-25T18:48:37Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118517#M9334</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thanks James,&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt; &lt;BR /&gt;I have seen a few people talk about IN_MEMORY, but I have never seen any help files or code examples using it, so I have never tried it out! Sounds like something I should look into!&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;BR /&gt;Stacy&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In addition to relief from the locking issue I was having, the in_memory space performs much better.&amp;nbsp; However, I do see that threre are limitations to it as well and it can be a double-edged sword as you will then be upping the RAM usage (I think).&amp;nbsp; But the idea is to do the processing there, then write out the final output to where you need the result(s) to be.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It is very easy to implement, just set your workspace to it and process as normal.&amp;nbsp; Here is an example of a def() I use before I start any processing to "clear" out the IN_MEMORY sapce (although, I am not so sure this is even required.&amp;nbsp; But because I am still unsure how garbage collection fully works when tools are being run from ArcCatalog in succession -- so I just clear it out anyway).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am implementing arcgisscripting(9.3) for ArcGIS 9.3.1 here&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

def ClearINMEM():
&amp;nbsp;&amp;nbsp; ## clear out the IN_MEMORY workspace of any featureclasses
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Workspace = "IN_MEMORY"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ### clear in_memory of featureClasses
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fcs = gp.ListFeatureClasses()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ### for each FeatClass in the list of fcs's, delete it.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for f in fcs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Delete_management(f)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage("deleted feature class: " + f)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ### clear in_memory of tables
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tables = gp.ListTables()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for tbl in tables:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Delete_management(tbl)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage("deleted table: " + tbl)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage("in_memory space cleared")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage("The following error(s) occured attempting to ClearINMEM " + gp.GetMessages())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:57:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118517#M9334</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T06:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118518#M9335</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Late to the party but....&lt;/P&gt;&lt;P&gt;Stacy, thanks for the post. &lt;/P&gt;&lt;P&gt;I've inherited some python code that makes use of your 10.0 ClearWSLocks so very helpful to find this "newer" version since we're still running servers on 10.1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just to add a contribution, the IN_MEMORY is very useful, IF you have sufficient RAM on the processing device.&lt;/P&gt;&lt;P&gt;I wrote a script to pickup a file geodB of roads for a large county (almost 1200 Sq Miles) and dump part of it out into an Excel file.&amp;nbsp; Using IN_MEMORY really sped it up.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Mar 2015 00:23:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118518#M9335</guid>
      <dc:creator>PaulDavidson1</dc:creator>
      <dc:date>2015-03-06T00:23:54Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118519#M9336</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Stacy,&lt;/P&gt;&lt;P&gt;I am trying to update a feature class by running a python script after hours.&lt;/P&gt;&lt;P&gt;Unfortunately I am having trouble with file locks caused by other users.&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-3 jive-image" src="https://community.esri.com/legacyfs/online/122932_pastedImage_2.png" style="max-height: 900px; max-width: 1200px;" /&gt;&lt;/P&gt;&lt;P&gt;I have tried your clearWSLocks.py, but it runs with this error:&lt;/P&gt;&lt;P&gt;&lt;IMG class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/122930_pastedImage_0.png" style="max-height: 900px; max-width: 1200px;" /&gt;&lt;/P&gt;&lt;P&gt;So it is complaining about this section of code:&lt;IMG class="image-2 jive-image" src="https://community.esri.com/legacyfs/online/122931_pastedImage_1.png" style="max-height: 900px; max-width: 1200px;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Do you have any idea what could be causing this?&lt;/P&gt;&lt;P&gt;I am using ArcGIS 10.2... not sure if that makes a difference or not.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for your help!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Aug 2015 01:25:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118519#M9336</guid>
      <dc:creator>ScottLinton</dc:creator>
      <dc:date>2015-08-19T01:25:04Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118520#M9337</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Scott, did you download psutil (or do you have it already?)&amp;nbsp; Looks like you can get it&lt;/P&gt;&lt;P&gt;&lt;A href="https://pypi.python.org/pypi/psutil/#downloads"&gt;https://pypi.python.org/pypi/psutil/#downloads&lt;/A&gt;​&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If that isn't imported, then is will have an issue with line 26 in your code above.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Aug 2015 02:46:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118520#M9337</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2015-08-19T02:46:22Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118521#M9338</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;Yes I have installed psutil... any other ideas?&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Aug 2015 02:54:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118521#M9338</guid>
      <dc:creator>ScottLinton</dc:creator>
      <dc:date>2015-08-19T02:54:09Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118522#M9339</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;psutil has changed the function names in my download. p.name is now a function p.name() and&lt;/P&gt;&lt;P&gt;p.get_open_files() is now p.open_files()&lt;/P&gt;&lt;P&gt;&lt;A href="https://pythonhosted.org/psutil/"&gt;documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This will work for processes on your machine, but I don't know how to close files open across a network.&lt;/P&gt;&lt;P&gt;This could happen if someone goes home with ArcMap open and their screen saver on. Anyone with ideas?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Aug 2015 05:12:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118522#M9339</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2015-08-19T05:12:24Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118523#M9340</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Scott,&lt;/P&gt;&lt;P&gt;Working with the suggestions from &lt;A href="https://community.esri.com/migrated-users/3269" target="_blank"&gt;Kim Ollivier&lt;/A&gt;​ I was able to get the program to run without error, although I'm not sure it did anything in my case.&amp;nbsp; I did add a print statement so you can see the PiD and process names it is processing.&amp;nbsp; For me, it didn't close my ArcMap or get rid of my locks, but I don't have anymore time to debug tonight.&amp;nbsp; I'll include the code as I have it now.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy&amp;nbsp; 
import os&amp;nbsp; 
import psutil&amp;nbsp; 

def clearWSLocks(inputWS):&amp;nbsp; 
&amp;nbsp; '''Attempts to clear ArcGIS/Arcpy locks on a workspace.&amp;nbsp; 
&amp;nbsp; Two methods:&amp;nbsp; 
&amp;nbsp;&amp;nbsp; 1: if ANOTHER process (i.e. ArcCatalog) has the workspace open, that process is terminated&amp;nbsp; 
&amp;nbsp;&amp;nbsp; 2: if THIS process has the workspace open, it attempts to clear locks using arcpy.Exists, arcpy.Compact and arcpy.Exists in sequence&amp;nbsp; 
&amp;nbsp; Notes:&amp;nbsp; 
&amp;nbsp;&amp;nbsp; 1: does not work well with Python Multiprocessing&amp;nbsp; 
&amp;nbsp;&amp;nbsp; 2: this will kill ArcMap or ArcCatalog if they are accessing the worspace, so SAVE YOUR WORK&amp;nbsp; 
&amp;nbsp; Required imports: os, psutil&amp;nbsp; 
&amp;nbsp; '''
&amp;nbsp; # get process ID for this process (treated differently)
&amp;nbsp; thisPID = os.getpid()
&amp;nbsp; # normalise path
&amp;nbsp; _inputWS = os.path.normpath(inputWS)
&amp;nbsp; # get list of currently running Arc/Python processes&amp;nbsp; 
&amp;nbsp; p_List = []
&amp;nbsp; ps = psutil.process_iter()
&amp;nbsp; for p in ps:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if ('Arc' in p.name()) or ('python' in p.name()):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("{0}, {1}".format(p.pid, p.name()))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p_List.append(p.pid)

&amp;nbsp; # iterate through processes&amp;nbsp; 
&amp;nbsp; for pid in p_List:
&amp;nbsp;&amp;nbsp;&amp;nbsp; p = psutil.Process(pid)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # if any have the workspace open
&amp;nbsp;&amp;nbsp;&amp;nbsp; if any(_inputWS in pth for pth in [fl.path for fl in p.open_files()]):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! Workspace open: %s' % _inputWS
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # terminate if it is another process 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if pid != thisPID:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! Terminating process: %s' % p.name()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p.terminate()&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! This process has workspace open...'&amp;nbsp; 

&amp;nbsp; # if this process has workspace open, keep trying while it is open...&amp;nbsp; 
&amp;nbsp; while any(_inputWS in pth for pth in [fl.path for fl in psutil.Process(thisPID).open_files()]):&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! Trying Exists, Compact, Exists to clear locks: %s' % all([arcpy.Exists(_inputWS), arcpy.Compact_management(_inputWS), arcpy.Exists(_inputWS)])&amp;nbsp; 
&amp;nbsp; return True&amp;nbsp; 

#######################################&amp;nbsp; 
## clearWSLocks usage example #########&amp;nbsp; 
#######################################&amp;nbsp; 
# define paths and names&amp;nbsp; 
workspace =&amp;nbsp; r"C:\temp\test.gdb" 
tableName = 'testTable'&amp;nbsp; 
# create table&amp;nbsp; 
arcpy.CreateTable_management(workspace, tableName)&amp;nbsp; 
# usage of clearWSLocks&amp;nbsp; 
clearWSLocks(workspace)&amp;nbsp; &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:57:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118523#M9340</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2021-12-11T06:57:06Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118524#M9341</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I recently came across a post or a blog that had a method (I think based in arcpy) for clearing locks.&amp;nbsp; But I cannot find the link or the sample code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I bring this up because my memory (faulty at best) tells me that it was using an arcpy function specifically for the task.&amp;nbsp; Hence it was almost a one liner.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ring any bells for anyone?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BTW - we've been using Stacy's clearWSLocks since it's early days when it was just a few lines of code.&lt;/P&gt;&lt;P&gt;Works great and is a staple component of our nightly python scripts.&lt;/P&gt;&lt;P&gt;Thanks Stacy!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Aug 2015 00:12:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118524#M9341</guid>
      <dc:creator>PaulDavidson1</dc:creator>
      <dc:date>2015-08-20T00:12:36Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118525#M9342</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You might be thinking of the tool in ArcCatalog for removing locks on multiuser enterprise databases such as SqlServer or Oracle. This is a different (and much easier problem) because the database is managing the locks, not Windoze. But there is no python scripting tool, it is just a GUI in ArcCatalog.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Aug 2015 22:03:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118525#M9342</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2015-08-20T22:03:37Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118526#M9343</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Kim...&lt;/P&gt;&lt;P&gt;That might be what I'm thinking of, except I thought I remembered seeing Python code.&lt;/P&gt;&lt;P&gt;I also thought I saved the code as a sample to somewhere safe for later review.&lt;/P&gt;&lt;P&gt;Obviously, it was too safe of a location.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Aug 2015 22:53:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118526#M9343</guid>
      <dc:creator>PaulDavidson1</dc:creator>
      <dc:date>2015-08-20T22:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118527#M9344</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;arcpy.RefreshCatalog() ?&lt;/P&gt;&lt;P&gt;Kim used to be a big fan &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Aug 2015 23:08:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118527#M9344</guid>
      <dc:creator>BruceHarold</dc:creator>
      <dc:date>2015-08-20T23:08:39Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118528#M9345</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Until it was removed at 10.x! I see it has returned to update the in-memory reference after a system copy so maybe it is just a null-op if the paths have not changed? It doesn't mention closing any open table locks so my assumption is that it doesn't.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It is certainly helpful to keep the Catalog up to date with the windows file system when using file geodatabases, which may lag due to various remote services, buffers, file lock indicators (which are just a temporary file) cleanup not kicking in fast enough during running a script.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Bruce can you help us there?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 20 Aug 2015 23:32:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118528#M9345</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2015-08-20T23:32:00Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118529#M9346</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;RefreshCatalog() doesn't have any lock handling functionality &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 21 Aug 2015 16:17:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118529#M9346</guid>
      <dc:creator>BruceHarold</dc:creator>
      <dc:date>2015-08-21T16:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118530#M9347</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As you mentioned correctly, it does not close any 'Arc' process(es). In my environment the "if any(..." expression returns never true because neither the personal gdb (*.mdb) nor its lock file are listed by p.open_files() object as being accessed. I did no try with *.gdb.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I assume, when I take a look at Microsoft's Access JetEngine, that not the "ArcMAP.exe" process itself is locking the file but some other handles will take this in charge. Unfortunately I did not find a way to jump over to such a process and get the needed identifying information.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A second aspect to consider may be the warning statement in "&lt;A href="https://pythonhosted.org/psutil/#psutil.Process.open_files" title="https://pythonhosted.org/psutil/#psutil.Process.open_files"&gt;https://pythonhosted.org/psutil/#psutil.Process.open_files&lt;/A&gt;" paragraph about Windows API liability. Well, this one seems not to be solvable quite soon despite taken efforts.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Either way no solution shows up for the moment here. Very frustrating...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any other ideas from around the world? &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Jan 2016 15:12:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118530#M9347</guid>
      <dc:creator>AndréVinko</dc:creator>
      <dc:date>2016-01-26T15:12:20Z</dc:date>
    </item>
    <item>
      <title>Re: Clearing Arc/Arcpy workspace locks</title>
      <link>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118531#M9348</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am using arcpy.gp.CreateSQLiteDatabase which must have some lock file somewhere whether on disk or memory as once the script ends, the file cannot be deleted or written to unless I exit ArcMap.&amp;nbsp; If I run the tool to overwrite the file, it won't because it says the file is in use.&amp;nbsp; I am trying to write to the DB it creates and this does not work unless i exit ArMap and then run the tool to write to the DB.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried this script in hopes it would fix my issue but I get a message:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Traceback (most recent call last):&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "D:\SAGE Development\ToolBox\raster2gpkg-master\raster2gpkg-master\GeoPackageToolbox.tbx#CreateSQLiteDatabase.py", line 90, in &amp;lt;module&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "D:\SAGE Development\ToolBox\raster2gpkg-master\raster2gpkg-master\GeoPackageToolbox.tbx#CreateSQLiteDatabase.py", line 85, in create_sqlite_database&lt;/P&gt;&lt;P&gt;&amp;nbsp; File "D:\SAGE Development\ToolBox\raster2gpkg-master\raster2gpkg-master\GeoPackageToolbox.tbx#CreateSQLiteDatabase.py", line 39, in clear&lt;/P&gt;&lt;P&gt;TypeError: argument of type 'instancemethod' is not iterable&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It seems to have issue with the if statement in&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if ('Arc' in p.name) or ('python' in p.name):&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Mar 2016 01:24:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/clearing-arc-arcpy-workspace-locks/m-p/118531#M9348</guid>
      <dc:creator>LukeCatania</dc:creator>
      <dc:date>2016-03-25T01:24:40Z</dc:date>
    </item>
  </channel>
</rss>

