title: How to delete all embedded objects in Word and PowerPoint using Open XML SDK?
body: <p>I am trying to delete all embedded object from Word and PowerPoint files using openxml SDK. I am new to Open XML and not sure whether I am doing this correctly. Below is the code I have. My intention is to remove any objects embedded and to delete images embedded. Both codes when executed are giving errors.</p>
  
  <p>Code that I tried to delete all embedded items in the document.</p>
  
  <pre><code>using (var wdDoc = WordprocessingDocument.Open(wordFilePath, true))
  {
      var docPart = wdDoc.MainDocumentPart;
      var document = docPart.Document;
      var embeddedObjectsCount = docPart.EmbeddedObjectParts.Count();
      while (embeddedObjectsCount &gt; 0)
      {
          docPart.DeletePart(docPart.EmbeddedObjectParts.FirstOrDefault());
          embeddedObjectsCount = docPart.EmbeddedObjectParts.Count();
      }
  }
  </code></pre>
  
  <p>Code that I tried to delete all images in the document. (This works partially if I don't have any objects embedded in the document.)            </p>
  
  <pre><code>using (var wdDoc = WordprocessingDocument.Open(wordFilePath, true))
  {
      var docPart = wdDoc.MainDocumentPart;
      var document = docPart.Document;
      var imageObjectsCount = docPart.ImageParts.Count();
      while (imageObjectsCount &gt; 0)
      {
          docPart.DeletePart(docPart.ImageParts.FirstOrDefault());
          imageObjectsCount = docPart.ImageParts.Count();
      }
  }
  </code></pre>
  
  <p>When I run the above code the file I use is getting corrupted. I would like to know how to remove all embedded objects from Word without corrupting the file.</p>
  
  <p>I haven't done anything on PowerPoint yet, but I hope it would be similar to Word document.</p>