Today I ran into an issue with SPWebConfigModification.

SPWebConfigModification can be used to make changes to SharePoint’s web.config. The advantage of using SPWebConfigModification - instead of manually changing web.config files on each server - is that changes are applied automatically to each Web Frontend Server (WFE) in the farm.

Internally SharePoint is holding a collection of modifications (a collection of SPWebConfigModification) that should be applied to the web.config File. Once a modification failed, this particular instance of SPWebConfigModification may resist within the collection. Therefore you should always clear the collection directly before staging your modifications on an instance of SPWebApplication.

The snippet below demonstrates it, right after receiving the webApp, WebConfigModifications.Clear() is invoked:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    var webApp = (SPWebApplication)properties.Feature.Parent;
    var myModification = new SPWebConfigModification("mode", "system.web/customErrors")
            Owner = properties.Feature.DefinitionId.ToString(),
            Sequence = 0,
            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute,
            Value = "Off"

Happy SharePoint’ing