Is there any way in leaflet to delete a specific type of features from a feature group (mixed content: markers, polygons, polylines)? I know this code that work: map.removeLayer(drawnItems); but it delete all the objects within the drawnItems featuregroup. I only need a specific type of features, for example all the markers to be deleted.

2 Answers 2


In a similar way to my answer to leaflet count by featuretypes in featuregroup :

  .filter(function(layer) {
    return layer instanceof L.Marker;
  .forEach(function(layer) {

Or in a shorter one-liner using arrow functions:

drawnItems.getLayers().filter(l=>l instanceof L.Marker).forEach(l=>map.remove(l));

Remember to check the documentation for Array.prototype.filter and Array.prototype.forEach.

On the other hand, you should consider creating several L.FeatureGroups, one for each kind of layer you want to add/remove from/to the map.

  • thank for your help and for the useful tips. I tested the code and it generates -drawnItems.filter is not a function- error for some reason. I tried to modify but no succes..
    – Tamas Kosa
    Commented Jun 21, 2017 at 12:17
  • Not "some reason". If drawnItems is an instance of L.LayerGroup, then it's not an array. OTOH, drawnItems.getLayers() returns an instance of Array, which has a filter method. Commented Jun 22, 2017 at 10:16
    // map.removeLayer(drawnFeatures); <-- this the wrong way

that can delete each layer without delete Features group. can be used again for new drawn items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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