Deleting features can be done in a single line of code following the format as shown:
LayerName.dataProvider().deleteFeatures([list of id])
In the previous code, you use deleteFeatures() and the id of the layer. The id is the feature.id(). It is a number held internally and not in a user assigned attribute. To get the id of a specific feature you can iterate through them as you learned earlier in this chapter. The following code shows you how to delete the feature we created in the previous section:
for x in scf.getFeatures():
if x["ID"]==311:
scf.dataProvider().deleteFeatures([x.id()])
The previous code iterates through the features in the layer looking for the one with the ID of 311. When it finds it, it uses deleteFeatures() and passes the id using x.id(). In this case the id was 216. If you know the id of the feature, you can delete it without the loop.
You can also pass a list of IDs as shown in the following code:
for x in scf.getFeatures():
if x["Status"]=='Closed':
key.append(x.id())
scf.dataProvider().deleteFeatures(key)
The previous code iterates through the features in the layer looking for all of the 'Closed' cases. When it finds one, it puts the id in the list key. Lastly, it calls deleteFeatures() and passes the list.