del layer

805
2
Jump to solution
01-16-2011 05:29 AM
KariGunnarsson
New Contributor
how do I delete a layer

my_layer = "some name"
my_layer_location = r"c:\some_location"

arcpy.MakeFeatureLayer_management(feature_in, my_layer)
arcpy.SaveToLayerFile_management(my_layer, my_layer_location)

del my_layer

...

my_layer = arcpy.mapping.Layer(my_layer_location)
my_layer.credits = "Kari Gunnarsson"


This will give an ERROR

How can I do this without geting the Error?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ChrisSnyder
Regular Contributor III
Try using the Delete_management() tool instead of the del statement:

arcpy.Delete_management(my_layer)

View solution in original post

0 Kudos
2 Replies
ChrisSnyder
Regular Contributor III
Try using the Delete_management() tool instead of the del statement:

arcpy.Delete_management(my_layer)
0 Kudos
NiklasNorrthon
Occasional Contributor III
Just some additional information about the python del-statement:

The del-statement doesn't really delete! It removes a name from a python namespace. A python name always refers to a python object. If the name is the only one refering a specific object that object might be deleted sooner or later (usually sooner, but there are exceptions).

In code del an_object is pretty much the same as an_object = None except that you get a name error if you try to use the name an_object before binding it to something else first.

Further: Even if an object is deleted it doesn't mean that an underlying resource (e.g. a file) is removed.