I am working in ArcGIS Pro 2.9 and I am attempting to remove a layer from the undo stack after the layer has been removed from the map. I do not want the user to have the ability to undo the removal of the layer and bring it back into the map. Once the layer has been removed then that is final. With that being said, I am successfully using the OperationManager object to remove the layer from the undo stack if the layer is removed programmatically. However, the same code does not work if the layer is removed from the GUI using the TOC. This doesn't make any sense that I could do what I need programmatically, but not from the GUI. I am using the LayersRemovedEvent to check for whether the layer has been added to the undo stack. It does when I call "map.RemoveLayer," but not when the event fires from a GUI removal from the TOC. Is this just the way that ArcGIS Pro is designed? Does the GUI removal of the layer from the TOC not add the layer to the undo stack until after the LayersRemovedEvent fires? If so, is there another way to get to the layer removal operation and remove it from the undo stack after the event?
The following is my method that removes operations from the undo stack:
public static void RemoveUndoOperations(string opName)
{
if (MapView.Active?.Map == null) { return; }
OperationManager opManager = MapView.Active.Map.OperationManager;
List<Operation> ops = opManager.FindUndoOperations(o => o.Name.Contains(opName));
foreach (Operation op in ops)
{
opManager.RemoveUndoOperation(op);
}
}