81

I have a Web.config with several ConnectionStrings

<connectionStrings>
    <add name="connStr1" connectionString="...
    <add name="ConnStr2" connectionString="...
    <add name="connStr3" connectionString="...

Is there a way using config transformations to remove a specific connectionstring? Something Like:

<connectionStrings>
    <xdt:Remove connStr2?

Obviously no where near the correct syntax, but you get my drift...

Didaxis
  • 8,486
  • 7
  • 52
  • 89

2 Answers2

124

This will remove a specific connection string based on its name.

<configuration>
  <connectionStrings> 
    <add name="ConnStr2" xdt:Transform="Remove" xdt:Locator="Match(name)" connectionString=" " /> 
  </connectionStrings> 
</configuration>

Note that the connectionString value is not empty string, but is instead a space. Any non-empty value would do.

Dmytro Shevchenko
  • 33,431
  • 6
  • 51
  • 67
hyke20
  • 1,241
  • 1
  • 7
  • 2
  • 2
    The 'DefaultConnection-Web.config Connection String' argument cannot be null or empty. Is what I get when I place your code inside my web.release.config. Any ideas? Thanks – Leigh Apr 09 '13 at 15:47
  • 9
    Why is the `connectionString` attribute required, at all? Shouldn't `xdt:Transform="Remove"` be able to remove the node based on its name only? – Dmytro Shevchenko Nov 23 '16 at 11:23
  • @DmytroShevchenko & Leigh - I don't get that error when I use hyke20's example, but if I leave off the 'connectionString' property I get a warning that it's missing. – Robotnik Feb 06 '17 at 06:39
  • You may get a warning if the attribute is not present, but the transformation should still apply correctly. I personally use `connectionString="any"` to be clear to future me that the transform doesn't care what the value of the connection string is. – Josh Gust Aug 16 '19 at 16:25
85

From the MSDN documentation on the subject:

<configuration xmlns:xdt="...">
  <connectionStrings>
    <add xdt:Transform="Remove" />
  </connectionStrings>
</configuration>

The Transform="Remove" is the magic you're looking for. There is also a Transform="RemoveAll" which you might be able to use in conjunction with a specific add(s).

EDIT

On second thought you may also be able to combine the Locator attribute with the Remove defined above to limit which elements you actually want to delete.

More definitively:

<configuration xmlns:xdt="...">
  <connectionStrings>
    <add xdt:Transform="Remove" xdt:Locator="XPath(configuration/connectionStrings[@name='ConnStr2'])" />
  </connectionStrings>
</configuration>

Or similar should work.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • 17
    The XPath won't work. The correct syntax is the one mentioned by @hyke20 bellow. To test the transformation online you can use: http://webconfigtransformationtester.apphb.com/ – Leniel Maccaferri Feb 07 '12 at 15:51
  • Why would you remove the actual namespace in the sample above? It is: xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform". Fully: [] – Nicholas Petersen Aug 10 '16 at 21:20
  • @AlexanderRyanBaggett Doing so limits the environment portability of the code which is the reason config transforms are used to begin with. There may be a way to do it but I'd suggest asking a new question. – M.Babcock May 04 '17 at 12:52