ArcGIS Pro 3.8 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / Map Class / RemoveLayers Method
An enumerable of layers
Example

In This Topic
    RemoveLayers Method (Map)
    In This Topic
    Removes the specified layers from the map. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public void RemoveLayers( 
       IEnumerable<Layer> layers
    )
    Public Sub RemoveLayers( _
       ByVal layers As IEnumerable(Of Layer) _
    ) 

    Parameters

    layers
    An enumerable of layers
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Example
    Removes all layers that are unchecked
    {
      await QueuedTask.Run(() =>
      {
        //Get the group layers first
        IReadOnlyList<GroupLayer> groupLayers = map.Layers.OfType<GroupLayer>().ToList();
        //Iterate and remove the layers within the group layers that are unchecked.
        foreach (var groupLyr in groupLayers)
        {
          //Get layers that not visible within the group
          var layers = groupLyr.Layers.Where(l => l.IsVisible == false).ToList();
          //Remove all the layers that are not visible within the group
          map.RemoveLayers(layers);
        }
    
        //Group Layers that are empty and are unchecked
        foreach (var group in groupLayers)
        {
          if (group.Layers.Count == 0 && group.IsVisible == false) //No layers in the group
          {
            //remove the group
            map.RemoveLayer(group);
          }
        }
    
        //Get Layers that are NOT Group layers and are unchecked
        var notAGroupAndUnCheckedLayers = map.Layers.Where(l => !(l is GroupLayer) && l.IsVisible == false).ToList();
        //Remove all the non group layers that are not visible
        map.RemoveLayers(notAGroupAndUnCheckedLayers);
      });
    }
    Remove elevation surface layers
    {
      //Ground will not be removed
      map.ClearElevationSurfaceLayers();
      ElevationSurfaceLayer surfaceLayer = map.GetElevationSurfaceLayers().FirstOrDefault(l => l.Name == "Surface2");
      //Cannot remove ground
      map.RemoveLayer(surfaceLayer);
      //Ground will not be removed
      map.RemoveLayers(map.GetElevationSurfaceLayers());
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also