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.

  • A feature layer is a lightweight representation of a feature class. Is there a specific reason it must be deleted?
    – blah238
    Oct 16, 2011 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, 2011 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, 2011 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, 2011 at 22:02

3 Answers 3



arcpy.env.overwriteOutput = True

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

  • 2
    should be arcpy.env.overwriteOutput = True
    – Josh Werts
    Jul 20, 2015 at 14:19

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"):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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