MakeFeatureLayer then Delete

787
5
09-12-2011 07:42 AM
JaysonLindahl
Occasional Contributor
I'm using python to create a feature layer and then delete the feature layer.  I don't think this is working for me, unless I am missing something.  My end goal is to delete features from an existing feature class based on some attribute values. 

I'm currently creating a feature layer by passing in a sql query.  I then dellete that feature layer. 

gp.MakeFeatureLayer(r"conn.sde/schema.fc", "flyr", vSqlStmnt)
gp.delete_management("flyr")


When complete, nothing is deleted.
Tags (2)
0 Kudos
5 Replies
DarrenWiens2
MVP Honored Contributor
You are creating a new feature layer, then deleting it in the next line (of course you won't see anything).

If you want to delete features, make a selection, then use Delete Features.
0 Kudos
JaysonLindahl
Occasional Contributor
I do want the feature layer I created to be deleted, however I want the deletion occur on the feature class from which the feature layer was created from.  Does that make sense? 

I did use the gp.deletefeatures, but that was taking a very, very long time to delete.  I was hoping that I could bulk delete better.

Should I create a feature layer, make a selection by attributes, then run delete_management or would I still need to use deletefeatures?
0 Kudos
DarrenWiens2
MVP Honored Contributor
Should I create a feature layer, make a selection by attributes, then run delete_management or would I still need to use deletefeatures?


Delete simply wipes the entire dataset, so you don't want that. If you're sure you've got the SQL right and it will select the correct features, then you should be able to run Delete Features straight on the feature class (once you've made the selection). My understanding for making the feature layer is so you're working on a copy. You can then overwrite the original feature class or save the layer to a new feature class.
0 Kudos
ChrisSnyder
Regular Contributor III
Note that ESRI made great strides in v10 getting this tool to delete features faster. Prior to v10, the tool (as you note) ran quite slowly, especially for large datasets accessed via a network or SDE.

I found that in v93, it was  often faster to just copy the records you wanted to keep to a new FC (use the Select_analysis tool) rather than deleting the ones you wanted to get rid of. Then just delete the original and rename the Select_analysis output FC to that of the original.
0 Kudos
LoganPugh
Occasional Contributor III
Delete simply wipes the entire dataset, so you don't want that. If you're sure you've got the SQL right and it will select the correct features, then you should be able to run Delete Features straight on the feature class (once you've made the selection). My understanding for making the feature layer is so you're working on a copy. You can then overwrite the original feature class or save the layer to a new feature class.


A feature layer is not a copy. It is a view. If you delete features from the feature layer, it also deletes them from the underlying feature class.

The recommended way to accomplish this is to use Make Feature Layer with your SQL WHERE clause, as you are doing in your first line. Then use DeleteFeatures_management to delete the selected features from the feature layer (which also deletes them from the underlying feature clsas).

Simply change Delete_management to DeleteFeatures_management in your code.
0 Kudos