Many a times, developers unknowingly add inappropriate values in the web config file of the application programatically.  Though it gets added, the next time when a SharePoint site is created, it throws error.  The problem is that, the inappropriate values are already saved in the database and in SharePoint its not suggested to make the changes in the datebase directly.  The api's in SPWebConfigModification comes handy in this scenario.

 

Here is the sample code to delete config values for the given web app.

 

 SPWebConfigModification oSPWebConfigModification = null;

 SPWebApplication oSPWebApplication = SPWebApplication.Lookup(new Uri(""));

 Collection<SPWebConfigModification> oCollection = oSPWebApplication.WebConfigModifications;

 for (int i = oCollection.Count- 1; i > -1; i--)

 {

      if (oCollection[i].Owner == "OwnerName") // Can be filtered using any of the property value. In this case, its the owner.

     {

          oSPWebConfigModification = oCollection[i];

          oCollection.Remove(oSPWebConfigModification);

          oSPWebApplication.Update();

          oSPWebApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

     }

 }