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– blah2382011年10月16日 20:11:16 +00:00Commented 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– Justin2011年10月16日 20:42:57 +00:00Commented 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– blah2382011年10月16日 21:54:25 +00:00Commented 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– blah2382011年10月16日 22:02:52 +00:00Commented Oct 16, 2011 at 22:02
3 Answers 3
arcpy.Delete_management(featureLayer)
-
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– blord-castillo2011年10月16日 21:12:09 +00:00Commented Oct 16, 2011 at 21:12
Including
arcpy.env.overwriteOutput = True
within your script will overwrite any previous version of a file without having to delete "bad" versions at all.
-
2should be
arcpy.env.overwriteOutput = True
Josh Werts– Josh Werts2015年07月20日 14:19:00 +00:00Commented 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.
try:
arcpy.MakeFeatureLayer_management(inFeatureClass, "outFeatureLayer", {whereClause})
except:
print arcpy.GetMessages()
finally:
if arcpy.Exists("outFeatureLayer"):
arcpy.Delete_management("outFeatureLayer")