Mapbox GL Js: adding and removing GeoJSON sources and layers

You should remove layer first then remove its source.

map.removeLayer(id);
map.removeSource(id);

in your case id = 'route'.


I just found out that the source is created with the same id as in the layer so:

map.removeSource('route')

Worked perfectly to completely remove both the layer and source.


...and check that the source/layer exist before you try to remove them

if (map.getLayer(id)) {
  map.removeLayer(id);
}
if (map.getSource(id)) {
  map.removeSource(id);
}

Tags:

Mapbox Gl Js