Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

When I used arcgisscripting to create in-memory output layer with gp.makefeaturelayer, I would put it in a try/except block and delete the gp object in the except piece when the makefeaturelayer would fail. Now when I am using arcpy, it looks like I need to close the idle window and open again in order to get the in-memory output layer to get deleted.

How could I have the output layer from my makefeaturelayer tool get deleted in a try/except block? Thanks.

share|improve this question
    
A feature layer is a lightweight representation of a feature class. Is there a specific reason it must be deleted? –  blah238 Oct 16 '11 at 20:11
    
when I am creating an in-memory feature layer and then next step fails..I make an adjustment and rerun the script in idle but then I get a "feature layer" already exists so I have to close idle down and restart..it takes extra time. –  Justin Oct 16 '11 at 20:42
    
Make Feature Layer doesn't create any data, only a view of some existing data. Perhaps you are creating an in-memory feature class instead? If so I would use Dan's suggestion to overwrite the in-memory feature class. It would also be a good idea to delete the in memory feature class when you no longer need it, because it will continue to take up memory until the application exits. –  blah238 Oct 16 '11 at 21:54
    
See Working with layers and table views in the help for a better explanation of what a feature layer is. –  blah238 Oct 16 '11 at 22:02
add comment

3 Answers

up vote 5 down vote accepted
arcpy.Delete_management(featureLayer)
share|improve this answer
    
This is exactly right per help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… "The Delete tool can be used to delete data in the in-memory workspace." –  blord-castillo Oct 16 '11 at 21:12
add comment

arcpy.overwriteOutputs = True

within your script will overwrite any previous version of a file without having to delete "bad" versions at all.

share|improve this answer
add comment

Deleting the feature layer in the except code block won't work. If you're in the except code block, the feature layer failed to be created.

I recommend adding a finally code block that will be executed whether the try block is sucessful or not. If you do this, you should make sure the feature layer exists before you can delete it.

try:
    arcpy.MakeFeatureLayer(inFeatureClass, "outFeatureLayer", {whereClause})
except:
    print arcpy.GetMessages()
finally:
    if arcpy.Exists("outFeatureLayer"):
        arcpy.Delete_management("outFeatureLayer")
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.