Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an OpenLayers layer with several vectors on it. I know the OpenLayers Feature ID (e.g. OpenLayers.Feature.Vector_241) and I would like to know how to remove this feature and only this feature?

I have considered deleting them all and then looping through an array to redraw them, but this seems a little inelegant, and long winded.

I have tried a variety of versions of the folllowing code:

layer.removeFeatures( featureID );


layer.removeFeatures( [featureID] );


layer.removeFeatures( {feature : featureID });


All help is welcome!



share|improve this question
up vote 17 down vote accepted

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:


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


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

layer.removeFeatures(layer.getFeatureBy('myId', myID));
share|improve this answer
I like the thinking! The problems is though that the "layer.getFeatureById(featureID))" statement returns null, and so the removeFeature() doesnt work. Im 100% that i have the right FeatureIDs though.... any more thoughts? – CatchingMonkey Sep 13 '11 at 14:23
Scratch that. I was using the wrong featureID. Im using your second example and it works perfectly. Nice one! – CatchingMonkey Sep 13 '11 at 14:43

This worked for me:


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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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