title: Remove Document variables for OpenXML DOCX?
body: <p>I'm trying to remove the <code>doc</code> variables from a DOCX file. Here is the code I'm using, but it doesn't remove any...</p>
  
  <p>This is the complete code:</p>
  
  <pre><code>class Program
      {
          static void Main(string[] args)
          {
              string filePath = "C:\\..\\..sample.docx";
              Remove removedocvars = new Remove();
              removedocvars.RemoveDocVariables(filePath);
  
          }
      }
  
  //method to remove doc vars
    public void RemoveDocVariables(string fileName)
          {
              using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true))
              {
  
                  List&lt;DocumentVariables&gt; DocVarsToDelete = doc.MainDocumentPart.RootElement.Descendants&lt;DocumentVariables&gt;().ToList();
                  foreach (DocumentVariables dc in DocVarsToDelete)
                  {
                      dc.Remove();
                  }
                  doc.MainDocumentPart.Document.Save();
              }
          }
  </code></pre>