Select to view content in your preferred language

ArcGIS 10.3.x Python 3.4 cannot delete layer

4745
7
11-13-2015 05:35 AM
FilipKrál
Frequent Contributor

Hello,

I need to create a layer, then delete it and then create a layer of the same name again. The script below works as expected in Python 2.7 32 bit, but fails in Python 3.4 64 bit in ArcGIS 10.3.1.

Can anyone explain why?

import arcpy

in_fc=r'C:\dump.gdb\zn3_0'
w = "STATION=3001"
lrname = 'lr1'

# create a layer
lr = arcpy.management.MakeFeatureLayer(in_fc, lrname, w).getOutput(0)
print(arcpy.Exists(lrname)) # True

# delete the layer
arcpy.management.Delete(lr)
arcpy.management.Delete(lrname)
del lr
print(arcpy.Exists(lrname)) # should be False but returns True in Python 3.4!

# create the layer again fails in Python 3.4
lr = arcpy.management.MakeFeatureLayer(in_fc, lrname, w).getOutput(0)
# ERROR 000725: Output Layer: Dataset lr1 already exists.

# and indeed the layer still exists after the error
print(arcpy.Exists(lrname)) # True
arcpy.management.GetCount(lrname).getOutput(0) # 1

Filip.

0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

arcpy.Delete_management won't delete a layer's name

https://pro.arcgis.com/en/pro-app/tool-reference/data-management/delete.htm

you might try to put in the optional data type to see how it handles it.

Help topic is from Pro, since I hope you are using the Pro version of arcpy with python 3.4 just in case there are differences.

0 Kudos
FilipKrál
Frequent Contributor

Hi,

On line 12 I delete the layer object (variable lr) and since that didn't work I added lines 13, 14 just to show all the things I tried and didn't help.

I tried setting data_type parameter to 'FeatureLayer' and also 'Layer' but that didn't help either. The help suggests the data_type parameter is "read only".

I am using the standalone python 3.4 that comes with ArcGIS Pro and I am executing this as a script file from windows command line.

F.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I assume you have tried the arcpy.overwriteOutput stuff? and is ArcGIS pro open? or ArcMap 10.3.x?  I never mix 2.7 or 3.4 python (even though it technically shouldn't matter...sort of...maybe) because they are two different streams if you examine how the python 3.4 and python 2.7 directories are set up.  I had an "issue" but I can't remember what it was, but that is when I got into the habit of keeping 32 bit with 32 bit and 64 bit with 64 bit...

0 Kudos
DennisJarrard
Esri Contributor

Can you reproduce this in the python window in Pro? In other words, if you attempt to delete a layer that exists within the map using the python window, does it work?

0 Kudos
FilipKrál
Frequent Contributor

I tried this in ArcPro Python window I now am even more puzzled.

When I pasted the code to the window and executed, all of the arcpy.Exists calls returned False. Even the first one so it suggested that the layer was never created.

I then tried running just the MakeFeatureLayer tool and this is what I got:

r = arcpy.management.MakeFeatureLayer(in_fc, lrname, w)

r.getMessages() # 'Start Time: Fri Nov 13 14:30:25 2015\nSucceeded at Fri Nov 13 14:30:25 2015 (Elapsed Time: 0.03 seconds)'

r.getOutput(0) # 'lr1'

arcpy.Exists('lr1') # False

arcpy.Exists(r.getOutput(0)) # False

What is going on here??

By the way, no layer was added to the table of contents either, not even when I run MakeFeatureLayer tool manually, but that is not an issue.

F.

0 Kudos
DanPatterson_Retired
MVP Emeritus

It doesn't have enough information i guess according to the help

Exists—Help | ArcGIS for Desktop

Input a dataset

requires:  The name, path, or both of a feature class, table, dataset, layer, shapefile, workspace, or file to be checked for existence.

Also the results are temporary

Make Feature Layer—Help | ArcGIS for Desktop

so I suspect that they aren't kept unless you add save to layer file in the process

For a test, try specifying the full name

import arcpy

# Set the current workspace

# arcpy.env.workspace = "c:/base/data.gdb"

0 Kudos
FilipKrál
Frequent Contributor

Hi,

I tried this in ArcGIS Pro 1.3 with Python 3.4.4 and it behaves well. The key thing is to delete the reference to the layer object when you are done with it ('del lr')!

So the right way to create and delete a layer is

in_fc, w =r'C:\temp\fc.shp', "FID < 3"
lr = arcpy.management.MakeFeatureLayer(in_fc, 'lr1', w).getOutput(0)
arcpy.management.Delete(lr)
del lr

Alternatively, wrapping the part of your where the layer is created, used, and then delted in a function *should* also resolve the issue because (as far as I know) the reference to the layer object *should* be garbage collected by Python when the function finishes.

The code below shows more details. It creates a layer, deletes the layer, then it creates and deltes the layer again. You would not do this in practice but it's important to be able to copletely delete layers in Python scripts (e.g. when using arcpy.management.SelectLayerByLocation in a loop). The bottom line is, use the 'del' statement.

import arcpy

in_fc=r'C:\temp\fc.shp'
w = "{0} < 3".format(arcpy.Describe(in_fc).OIDFieldName)
lrname = 'lr1'

# create a layer
lr = arcpy.management.MakeFeatureLayer(in_fc, lrname, w).getOutput(0)
print(arcpy.Exists(lr)) # True
print(arcpy.Exists(lrname)) # True
print(type(lr)) # <class 'arcpy._mp.Layer'>

# delete the layer
arcpy.management.Delete(lr)
print(arcpy.Exists(lr)) # True
print(arcpy.Exists(lrname)) # True

del lr

print(arcpy.Exists(lrname)) # False
#print(arcpy.Exists(lr)) # we deleted variable lr using 'del lr' so we can't refer to it

# create and delete the layer again
lr = arcpy.management.MakeFeatureLayer(in_fc, lrname, w).getOutput(0)
print(arcpy.Exists(lrname)) # True
print(arcpy.management.GetCount(lrname).getOutput(0)) # 3
arcpy.management.Delete(lr)
del lr

Filip.

0 Kudos