Remove/Delete a feature from OpenLayers vector layer

I don't think that OpenLayers is capable of removing a feature by providing the featureID. It seems like it can only remove a features by providing a feature or an array of features:

layer.removeFeatures(featureObject);

But you could instead locate the feature first and then remove the found feature:

layer.removeFeatures(layer.getFeatureById(featureID));

Or similar if its not the OpenLayers FeatureID you have, then

layer.removeFeatures(layer.getFeatureBy('myId', myID));

For OpenLayers v6+ it's done in another way, considering that layer is a VectorLayer:

let feature = layer.getSource().getFeatureById('featureID');

getFeatureById()


This worked for me:

*layer.removeFeatures(featureObject);*

But i've got the feature as a global variable, maybe in other cases you should try to keep the feature ID in order to search to remove it.

Tags:

Openlayers 2