Take the 2-minute tour ×

I am using the following code to remove a web.config modification which added to web.config via one feature

        Collection<SPWebConfigModification> applicationModifications = webApplication.WebConfigModifications;
        SPWebConfigModification browserCapsConfig = null;

        int modsCount = applicationModifications.Count;
        for (int i = modsCount - 1; i > -1; i--)
            if (applicationModifications[i].Owner == "OWNER_NAME")
                browserCapsConfig = applicationModifications[i];

        if (browserCapsConfig != null)

All web.config modifications are retrieved in collection properly but the thing is while removing it, it is removed from the collection (unable to retrieve it anymore) but not from the web.config. The added modification still there. I tried the same code in SharePoint feature and a console application and getting the same behavior.

Anything missing I should do to remove it from the web.config file?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

There's nothing wrong with your code above, so this almost certainly has something to do with your SPWebConfigModification object's "Name" value.

The SPWebConfigModification.Name property (and the Name parameter in the SPWebConfigModification's constructor) are actually XPath expressions that uniquely identify the config entry. SharePoint uses this XPath expression to locate the modification when you try to remove or update it, hence if you haven't set it properly then it can't be removed.

See these resources for more info and correct naming conventions:



share|improve this answer
I changed the Name to be like this configModification.Name = "add[@name='NAME']"; but I'm still getting the same behavior! Any suggestions? –  Ahmed Feb 29 '12 at 10:21
Something to always keep in mind with these programmatic config changes is that SharePoint will only ever remove what it has placed there itself. I.e. if you put in a config entry by hand (or update it) then the actual file is no longer in sync with the config modifications in the content db and you'll never remove it. Download the free "SharePoint Manager 2010 V2" tool, this allows you to drill down and see exactly what config changes are in the content database, plus their proper names etc. that'll tell you straight away if what mods SharePoint "thinks" are in your config file. –  Nick Larter Feb 29 '12 at 14:17
In the same vein, you should try blowing away your site and re-inserting all your config changes from scratch. As soon as things get "out of sync" it's generally a lost cause. –  Nick Larter Feb 29 '12 at 14:26
Worked for me... first I used xpath to target the nodes I wanted to remove and then, when that wasn't enough, I 'reset' my web.config by manually deleting the nodes so my code could add fresh new nodes. –  Aaronster Oct 29 '13 at 19:57
add comment

Your Answer


