Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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
up vote 8 down vote accepted
share|improve this answer
This is exactly right per… "The Delete tool can be used to delete data in the in-memory workspace." – blord-castillo Oct 16 '11 at 21:12

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.

    arcpy.MakeFeatureLayer_management(inFeatureClass, "outFeatureLayer", {whereClause})
    print arcpy.GetMessages()
    if arcpy.Exists("outFeatureLayer"):
share|improve this answer


arcpy.env.overwriteOutput = True

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

share|improve this answer
should be arcpy.env.overwriteOutput = True – jwerts Jul 20 '15 at 14:19

Your Answer


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.