ArcGIS Pro 3.1 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / Map Class / RemoveLayer Method
A layer.
Example

In This Topic
    RemoveLayer Method (Map)
    In This Topic
    Removes a specified layer from the map. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public void RemoveLayer( 
       Layer layer
    )
    Public Sub RemoveLayer( _
       ByVal layer As Layer _
    ) 

    Parameters

    layer
    A layer.
    Exceptions
    ExceptionDescription
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Example
    Removes all layers that are unchecked
    var map = MapView.Active.Map;
    if (map == null)
      return;
    //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 groupLayer in groupLayers)
    {
      //Get layers that not visible within the group
      var layers = groupLayer.Layers.Where(l => l.IsVisible == false).ToList();
      //Remove all the layers that are not visible within the group
      await QueuedTask.Run(() => 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
        await QueuedTask.Run(() => 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
    await QueuedTask.Run(() => map.RemoveLayers(notAGroupAndUnCheckedLayers));
    Remove empty groups
    var map = MapView.Active.Map;
    if (map == null)
      return;
    //Get the group layers
    IReadOnlyList<GroupLayer> groupLayers = map.Layers.OfType<GroupLayer>().ToList();
    foreach (var group in groupLayers)
    {
      if (group.Layers.Count == 0) //No layers in the group
      {
        //remove the group
        await QueuedTask.Run(() => map.RemoveLayer(group));
      }
    }
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3.0 or higher.
    See Also