<?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 When to set del cursor? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/when-to-set-del-cursor/m-p/1124338#M63195</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a very simple question in regards to the del cursor. I use it from time to time but I have not thought about it.&lt;/P&gt;&lt;P&gt;When is the del cursor used? I have a sample script with a del cursor but I am not sure if I used it in the right manner.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;edit.startEditing(False, True)
edit.startOperation()

with updateLines as cursor:
    for row in updateLines:
        if row[0] in UpdateIsolatedIDs_Lines:
            IsolationID = UpdateIsolatedIDs_Lines[row[0]]
            print (row)
            #print (IsolationID)
            if IsolationID:
                row[0] = row[0]
                row[1] = IsolationID
                row[2] = row[2]
                updateLines.updateRow(row)
            print (row)
            print ('\n')

    del cursor
    
edit.stopOperation()
edit.stopEditing(True)&lt;/LI-CODE&gt;&lt;P&gt;I am not sure if the del cursor is used properly but I just wanted to ask.&lt;/P&gt;</description>
    <pubDate>Thu, 09 Dec 2021 14:51:45 GMT</pubDate>
    <dc:creator>RPGIS</dc:creator>
    <dc:date>2021-12-09T14:51:45Z</dc:date>
    <item>
      <title>When to set del cursor?</title>
      <link>https://community.esri.com/t5/python-questions/when-to-set-del-cursor/m-p/1124338#M63195</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a very simple question in regards to the del cursor. I use it from time to time but I have not thought about it.&lt;/P&gt;&lt;P&gt;When is the del cursor used? I have a sample script with a del cursor but I am not sure if I used it in the right manner.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;edit.startEditing(False, True)
edit.startOperation()

with updateLines as cursor:
    for row in updateLines:
        if row[0] in UpdateIsolatedIDs_Lines:
            IsolationID = UpdateIsolatedIDs_Lines[row[0]]
            print (row)
            #print (IsolationID)
            if IsolationID:
                row[0] = row[0]
                row[1] = IsolationID
                row[2] = row[2]
                updateLines.updateRow(row)
            print (row)
            print ('\n')

    del cursor
    
edit.stopOperation()
edit.stopEditing(True)&lt;/LI-CODE&gt;&lt;P&gt;I am not sure if the del cursor is used properly but I just wanted to ask.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Dec 2021 14:51:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/when-to-set-del-cursor/m-p/1124338#M63195</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2021-12-09T14:51:45Z</dc:date>
    </item>
    <item>
      <title>Re: When to set del cursor?</title>
      <link>https://community.esri.com/t5/python-questions/when-to-set-del-cursor/m-p/1124344#M63196</link>
      <description>&lt;P&gt;This way is incorrect.&lt;/P&gt;&lt;P&gt;When you use a with block, this is what happens (on a very, very conceptual level!)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(...) as cursor:
    your_code()

# this gets translated into
cursor = arcpy.da.UpdateCursor(...)
cursor.__enter__()
your_code()
cursor.__exit__()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The __exit__ method of the cursors resets iteration and removes locks. When you delete the cursor object before the __exit__ method is called, there's a good chance that you'll get persistent locks.&lt;/P&gt;&lt;P&gt;Deleting the cursor object is optional. I use cursors all the time and never delete them, never had a problem. ESRI recommends considering using the del to be absolutely sure there are no locks left:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/de/pro-app/latest/arcpy/get-started/data-access-using-cursors.htm" target="_blank" rel="noopener"&gt;https://pro.arcgis.com/de/pro-app/latest/arcpy/get-started/data-access-using-cursors.htm&lt;/A&gt;&amp;nbsp;&lt;BR /&gt;Cursors support &lt;SPAN class=""&gt;with&lt;/SPAN&gt; statements to reset iteration and aid in removal of locks. However, using a &lt;SPAN class=""&gt;del&lt;/SPAN&gt; statement to delete the object or wrapping the cursor in a function to have the cursor object go out of scope should be considered to guard against all locking cases.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;If you want to use it, delete OUTSIDE of the with block:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(...) as cursor:
    for row in cursor:
        stuff()
del cursor&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Dec 2021 15:12:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/when-to-set-del-cursor/m-p/1124344#M63196</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2021-12-09T15:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: When to set del cursor?</title>
      <link>https://community.esri.com/t5/python-questions/when-to-set-del-cursor/m-p/1124576#M63215</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341"&gt;@JohannesLindner&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I hadn't used them to the degree that some people do. As a matter of fact, I mostly use them with a search cursor. I had noticed that most of my script was getting locked up for some reason and taking as long as several hours. I tried to see if freeing up and locks and emptying dictionaries and lists that I compiled to see if that would speed up the processing a bit. Thank you very much for informing me.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Dec 2021 20:18:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/when-to-set-del-cursor/m-p/1124576#M63215</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2021-12-09T20:18:37Z</dc:date>
    </item>
  </channel>
</rss>

