0

I have a azure function app which will generate multiple entries based on one partition key. In case any error during the function execution. I first delete all entities on my azure table which have the same partition key, in case the function get retries 2nd or 3nd times and insert duplicated records.

So I decided to use ExcuteBatch operation by looping through a set of 100s entities.

I not sure what the TableBatch will return. Does it return all success deletion result?

This is what i have written at this stage.

var projectionQuery = new TableQuery()
            .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "partitionKey"))
                .Select(new[] { "RowKey" });

        var entities = table.ExecuteQuery(projectionQuery).ToList();
        var offset = 0;
        while (offset < entities.Count)
        {
            var batch = new TableBatchOperation();
            var rows = entities.Skip(offset).Take(100).ToList();
            foreach (var row in rows)
            {
                batch.Delete(row);
            }

            var result = table.ExecuteBatch(batch);

            offset += result.Where(x=>x.HttpStatusCode == (int) HttpStatusCode.NoContent).Count() + offset;
        }

If the above code is correct. Will there be an infinite loop where the data which tried to delete could not be deleted and it keep tried to delete.

LittleFunny
  • 8,155
  • 15
  • 87
  • 198

1 Answers1

0

No. Your code will exit out of the loop as soon as any batch operation fails. Basically following line of code will throw an exception if there's a failure in deleting any of the entity in the batch.

var result = table.ExecuteBatch(batch);

One more thing I noticed in the following line of code:

offset += result.Where(x=>x.HttpStatusCode == (int) HttpStatusCode.NoContent).Count() + offset;

Why are you adding offset again towards the end of the expression when you're already doing offset +=?

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Sorry didn't realize the plus sign in between. If I remove the offset, does that statement make sense. – LittleFunny Aug 07 '19 at 21:50
  • If you put the `ExecuteBatch` and the next line where you change the offset in it's own `try/catch` block, then you will have a situation of infinite loop. Otherwise the code will break out of loop as soon as it fails to delete any batch. HTH. – Gaurav Mantri Aug 08 '19 at 04:29