<?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: Cannot delete temp gdb due to lock from executing program in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1179950#M64705</link>
    <description>&lt;P&gt;If you have some data structure hanging around in memory like for example a cursor on the data then you need to delete it to remove the lock, for example if it's called "rows" then you say "del rows". Here is a sample&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;cursor = arcpy.SearchCursor("roads", '"TYPE" &amp;lt;&amp;gt; 4')
for row in cursor:
    print("Name: {0},  CFCC code: {1}".format(row.NAME, row.CFCC))

del cursor, row&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 03 Jun 2022 21:56:01 GMT</pubDate>
    <dc:creator>Brian_Wilson</dc:creator>
    <dc:date>2022-06-03T21:56:01Z</dc:date>
    <item>
      <title>Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1179941#M64704</link>
      <description>&lt;P&gt;I am having trouble with the last phase of a script for a script tool: I cannot figure out how to delete my temporary gdb to close out the script. Having the program that's executing the script open retains a lock in the gdb...for instance when I close Jupyter (or VS code, or IDLE) I can see the lock file in the gdb disappear. How can I construct my code so this isn't an issue when made into a script tool?&lt;/P&gt;&lt;P&gt;There have a been a few other posts on this topic and none of those solutions seem to be quite right. The general advice has been to add an arcpy.env.overwriteOuput = True, and that has not been effective.&lt;/P&gt;&lt;P&gt;The code that creates the gdb:&lt;/P&gt;&lt;PRE&gt;#Create temporary Workspace&lt;BR /&gt;arcpy.AddMessage("\nCreating temporary workspace")&lt;BR /&gt;tempName = "incorp" + outVersion + "_TEMP"&lt;BR /&gt;tempGDB = os.path.join(outFolder, tempName + ".gdb")&lt;BR /&gt;arcpy.management.CreateFileGDB(outFolder, tempName)&lt;BR /&gt;arcpy.env.workspace = tempGDB&lt;BR /&gt;arcpy.env.overwriteOutput = True&lt;/PRE&gt;&lt;P&gt;The script then goes on and does several operations within that temp gdb, clipping, erasing, querying, adding fields, etc. Then when I go to delete it...&lt;/P&gt;&lt;PRE&gt;#Delete intermediate data&lt;BR /&gt;arcpy.AddMessage("\nDeleting intermediate data")&lt;BR /&gt;todel = (tempGDB, union, lastIncorp_xml)&lt;BR /&gt;for dataset in todel:&lt;BR /&gt;if arcpy.Exists(dataset):&lt;BR /&gt;arcpy.management.Delete(dataset)&lt;/PRE&gt;&lt;P&gt;I get ERROR 000601 -&amp;nbsp;&lt;SPAN&gt;Cannot delete [path].gdb. May be locked by another application. Failed to execute (Delete).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Any thoughts on what can be done so the tool can clean up after itself?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2022 21:30:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1179941#M64704</guid>
      <dc:creator>SFM_TravisBott</dc:creator>
      <dc:date>2022-06-03T21:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1179950#M64705</link>
      <description>&lt;P&gt;If you have some data structure hanging around in memory like for example a cursor on the data then you need to delete it to remove the lock, for example if it's called "rows" then you say "del rows". Here is a sample&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;cursor = arcpy.SearchCursor("roads", '"TYPE" &amp;lt;&amp;gt; 4')
for row in cursor:
    print("Name: {0},  CFCC code: {1}".format(row.NAME, row.CFCC))

del cursor, row&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2022 21:56:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1179950#M64705</guid>
      <dc:creator>Brian_Wilson</dc:creator>
      <dc:date>2022-06-03T21:56:01Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1179961#M64706</link>
      <description>&lt;P&gt;Agreed. Or better yet, use it in a with statement so it gets cleaned up even if there's an error before it can del.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.SearchCursor("roads", '"TYPE" &amp;lt;&amp;gt; 4') as cursor:
    for row in cursor:
        print("Name: {0},  CFCC code: {1}".format(row.NAME, row.CFCC))&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 03 Jun 2022 22:12:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1179961#M64706</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2022-06-03T22:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1179962#M64707</link>
      <description>&lt;P&gt;Although I've not tested it, I heard &lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/compact.htm" target="_self"&gt;Compact&lt;/A&gt; might clear out erroneous locks.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2022 22:13:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1179962#M64707</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2022-06-03T22:13:01Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1180200#M64709</link>
      <description>&lt;P&gt;If it's temp data, consider using the memory workspace so it's not writing to disk.&amp;nbsp; There is the arcpy environment's scratch database you can use as well, but you'll still have to manually delete the items.&lt;/P&gt;&lt;P&gt;If your two other items (union, lastIncorp_xml) in the todel tuple are datasets in the gdb that you are trying to delete, it could be creating a lock when the tuple is created, prohibiting the deletion of the database. If they are just references (i.e. assigned to gp output), you can 'del' them, and delete the tempGDB, or maybe try reordering the items to delete.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jun 2022 13:48:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1180200#M64709</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-06-06T13:48:39Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1180635#M64722</link>
      <description>&lt;P&gt;@Anonymous User&amp;nbsp;I am not an expert pythonista, so I am not sure if my workflows are best practices, but I wanted to write to disk so that I can verify intermediate data as I work downstream. This seems like a reasonable thing to keep in the workflow so if there are problems with the script's execution later it can maybe be troubleshooted (troubleshot?).&amp;nbsp;&lt;/P&gt;&lt;P&gt;union is in the tempGDB, lastIncorp_xml is not. When I have the contents of the gdb open in File Explorer I notice the lock file disappear when the program executing python is closed, so there's something about&amp;nbsp;&lt;EM&gt;accessing&amp;nbsp;&lt;/EM&gt;it in this way that creates the lock file (which supports your argument for doing it in memory, but I would like it on disk if I can). I have tried re-ordering items in the deletion but to no avail.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will try to put them in as references and se if 'del' works.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 15:08:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1180635#M64722</guid>
      <dc:creator>SFM_TravisBott</dc:creator>
      <dc:date>2022-06-07T15:08:22Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1180664#M64723</link>
      <description>&lt;P&gt;New theory. You are using several calls into geoprocessing tools, clip, query, etc and you have no control over the fact that one or more of them are called from your python process and are holding onto the file lock. That's a tough one. Since the Esri code is running in the same space as your calling code, their code won't clean up until your main program ends.&lt;/P&gt;&lt;P&gt;If this is true then you'd need to call the errant tool in a subprocess so that it would exit cleanly. That would be a pain. You'd need to figure out which tool(s) needed to be isolated. You'd need to learn about executing subprocesses. Not a bad thing to know really but still a pain. There is a lot more overhead while the subprocess loads a copy of python, loads the packages blah blah blah and then loads the tool and runs it.&lt;/P&gt;&lt;P&gt;I think you should try JeffK's idea first, use in_memory -- and it's much faster. Leave the existing on-disk code in there and comment it out so you can easily put it back if you need debugging. Ha -- or put in a "copy feature class" to write the intermediate data to disk once the processing step is done. That would bypass the lock file issue, let you combine several steps working in memory for more speed, and you could only copy to disk when you need to examine intermediate results.&lt;/P&gt;&lt;P&gt;I think Esri probably sidesteps this issue with Model Builder because each little colored box invokes a new process? I stopped using Model Builder a few years ago so I never tell people to use it anymore.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 15:57:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1180664#M64723</guid>
      <dc:creator>Brian_Wilson</dc:creator>
      <dc:date>2022-06-07T15:57:23Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1180672#M64724</link>
      <description>&lt;P&gt;Your windows explorer could be stopping the deletion because the gdb is 'open'/ being used by another process. Sounds flaky but it happens because you are trying to delete a 'folder', but its being viewed by windows explorer and it throws a fit.&lt;/P&gt;&lt;P&gt;Yeah, do your work in memory and then use CopyFeatures() wherever/whenever you want to export as a QC dataset as Brian laid out. I do it all the time during script building and when it is running correctly, I comment out the export statements.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 16:20:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1180672#M64724</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-06-07T16:20:55Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1180704#M64726</link>
      <description>&lt;P&gt;It's not explorer; I've isolated that one.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will reconfigure to work in memory and see how that goes. I built this following the model of another script that creates a temporary workspace and as far as I can tell I've followed it fine but maybe there's a detail I'm missing.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 17:02:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1180704#M64726</guid>
      <dc:creator>SFM_TravisBott</dc:creator>
      <dc:date>2022-06-07T17:02:44Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1188382#M64871</link>
      <description>&lt;P&gt;I am still not fully clear what was holding it open but the "memory" option worked fine.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;#Set memory workspace&lt;/SPAN&gt;
arcpy.AddMessage(&lt;SPAN class=""&gt;"\nCreating temporary workspace"&lt;/SPAN&gt;)
memory = &lt;SPAN class=""&gt;r"memory"&lt;/SPAN&gt;
arcpy.env.workspace = memory&lt;BR /&gt;
&lt;SPAN class=""&gt;#Clip by boundary layer&lt;/SPAN&gt;
arcpy.AddMessage(&lt;SPAN class=""&gt;"\nClipping to state boundary"&lt;/SPAN&gt;)
boeClipped = os.path.join(memory, &lt;SPAN class=""&gt;"boeClipped"&lt;/SPAN&gt;)
arcpy.analysis.Clip(boeDataSet, boundary, boeClipped)&lt;/PRE&gt;&lt;P&gt;For those out there that are novices like me: you still have to delete the layers created in memory, otherwise you can't run the tool again as it'll throw errors saying items exist. And apparently you can't set overwrite to True if using memory.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 17:07:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1188382#M64871</guid>
      <dc:creator>SFM_TravisBott</dc:creator>
      <dc:date>2022-06-30T17:07:09Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1188427#M64872</link>
      <description>&lt;P&gt;That is good that you found a solution, nice work!&amp;nbsp; I do find it odd that it requires you to still delete the items because the memory workspace only exists for as long as the script is running. Once the script completes, the memory workspace should be deleted and the RAM used for the memory workspace released. You may try the legacy 'in_memory' workspace too. There are some differences between the two and one may work better than the other for certain tasks.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 18:50:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1188427#M64872</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-06-30T18:50:32Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1331970#M68752</link>
      <description>&lt;P&gt;I've just also gone through this same painful experience. I had my search cursor inside a with, tried deleting all possible variables that might have had a reference to the to temporary file geodatabase (fgdb). This didn't work and using the memory workspace wasn't an option since i was using some gp tools that don't support this. The solution: to individually delete each tmp feature class I'd created in the tmp fgdb before deleting the tmp fgdb itself.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Sep 2023 14:14:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1331970#M68752</guid>
      <dc:creator>DarrenSmith</dc:creator>
      <dc:date>2023-09-25T14:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: Cannot delete temp gdb due to lock from executing program</title>
      <link>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1365634#M69525</link>
      <description>&lt;P&gt;Thank you for contributing to this discussion,&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/25169"&gt;@DarrenSmith&lt;/a&gt;. Your solution of deleting the objects in the fgdb before trying to delete the fgdb worked for me when I encountered this issue. I used &lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/delete.htm" target="_self"&gt;Delete&lt;/A&gt; for both the objects and the fgdb. In my case, &lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/compact.htm" target="_self"&gt;Compact&lt;/A&gt; was not necessary.&lt;/P&gt;&lt;P&gt;For reference, I was getting this error when trying to &lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/delete.htm" target="_self"&gt;Delete&lt;/A&gt;&amp;nbsp;the fgdb:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;arcgisscripting.ExecuteError: ERROR 000601: Cannot delete&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;And I was getting this error when the &lt;A href="https://docs.python.org/3/library/tempfile.html#tempfile.TemporaryDirectory" target="_self"&gt;Python&amp;nbsp;&lt;/A&gt;&lt;SPAN&gt;&lt;A href="https://docs.python.org/3/library/tempfile.html#tempfile.TemporaryDirectory" target="_self"&gt;TemporaryDirectory()&lt;/A&gt; was trying to clean up:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;PermissionError: [WinError 32] The process cannot access the file because it is being used by another process&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;...which caused the following:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;NotADirectoryError: [WinError 267] The directory name is invalid&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2024 23:46:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cannot-delete-temp-gdb-due-to-lock-from-executing/m-p/1365634#M69525</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2024-01-02T23:46:11Z</dc:date>
    </item>
  </channel>
</rss>

