title: Best Practice in Deleting Azure Table Entities in ForEach Loop
body: <p>I have not had consistent results when deleting entities in an Azure table when the deletion occurs in a “foreach” loop.  PartitionKey is unique.</p>
  
  <p>Is there a best practice that folks can recommend?</p>
  
  <p>I have left out Try..Catch statements for brevity.</p>
  
  <p>Assuming:</p>
  
  <pre><code>var context = new FooContext(_storageAccount);
  
          var query = (from e in context.TableBar
                                  where e.RowKey == rowKey
                                  select e).AsTableServiceQuery();
  </code></pre>
  
  <p>Method #1</p>
  
  <pre><code>                foreach (var entity in query.Execute())
              {
                      // delete each entity
                      context.DeleteObject(entity);
                      context.SaveChanges();
              }
  </code></pre>
  
  <p>Method #2</p>
  
  <pre><code>            foreach (var entity in query.Execute())
              {
                      // delete each entity
                      context.DeleteObject(entity);
              }
              context.SaveChanges();
  </code></pre>
  
  <p>Method #3</p>
  
  <pre><code>            var bars = query.Execute();
              foreach (var bar in bars)
                  context.DeleteObject(bar);
              context.SaveChanges();
  </code></pre>
  
  <p>Method #1 appears to delete most entities, but generally the last entity does not delete.  It’s a valid entity.</p>