Select to view content in your preferred language

How to remove Lock?

2481
1
08-29-2013 10:28 PM
ZuoqiChen
Emerging Contributor
I creat a loop.
In the loop, it makes a HillShade Raster (named A) and makes a new Raster (named B) from A.
But the Raster A can not be delete because the Raster A is Locked.
How can i solve this?
Tags (2)
0 Kudos
1 Reply
XanderBakker
Esri Esteemed Contributor
Hi Zuoqi Chen,

Having a dataset locked can be a difficult thing to solve. IMHO I thing the locking mechanism in ArcGIS has been implemented to drastic, causing more problems than it avoids...

I assume you are writing your raster datasets to a file geodatabase. It is possible to test the scema locking before deleting and place it in a "try except" (as shown below), but this doesn't solve anything if you have a lock on your dataset. Always be sure to delete any references to your dataset and delete cursors, rows, etc...

if arcpy.TestSchemaLock(data):
    try:
        arcpy.Delete(data)
    except:
        # catch error here
 else:
     print "Unable to acquire schema lock"


source: http://resources.arcgis.com/en/help/main/10.2/index.html#/TestSchemaLock/018v0000002m000000/


What I have also come across is using the garbage collector. This may sometimes solve the problem. See snippet below:

    # at beginning of script
    import gc # Garbage Collector
    gc.collect()

    # at end of script
    collected = gc.collect()
    print "Garbage collector: collected %d objects." % (collected)

    # try to delete now...


sources:
http://gis.stackexchange.com/questions/19408/arcgis-10-0-python-searchcursor-file-locking
http://docs.python.org/2/library/gc.html
http://pymotw.com/2/gc/


If this doesn't work, you could go back to the gold old Esri grid data format (write raster to folder), which is probably one of the last formats for which the locking mechanism hasn't been implemented. 😄 This did solve some nasty locking problems I had...
0 Kudos