When you want to make changes to the web.config file of your SharePoint 2010 application, you can use two methods:
- Create a webconfig.<your_name>.xml file that you add to the \CONFIG directory (Source on MSDN)
- Do it programmatically and use the SPWebConfigModification and SPWebApplication.WebConfigApplication objects (Source on MSDN).
However, when you use the second method and remove the changes from the file using Remove(), the changes are not removed. Even if you use .Update() and .ApplyWebConfigModifications(). This may only result in your modification appearing multiple times in the web.config file, but if you insert more sensitive modifications to uniqueness, such as for an HttpModule, it creates an error that prevents the site from functioning (Error 500).
A question asked in this regard on the MSDN forums reveals that this is a known bug from Microsoft (Office14 831708) and that it should eventually be fixed. In the meantime, you can try this PowerShell script, but in my case, it did not work!
Good luck!