Search code examples
azure-cognitive-searchazure-search-.net-sdk

Azure Search SDK for Blob Storage - Deleting Files


I have created an application that lists all the documents in an Azure storage container, and lets the user mark specific files to delete.

This is an Azure Search application, so the process is to add a "deleted" metadata property to the selected files, run the indexer to remove that information from the index, and then physically delete the files.

Here's the code for that process:

serviceClient.Indexers.Run(documentIndexer);

var status = serviceClient.Indexers.GetStatus(documentIndexer).LastResult.Status;

// Loop until the indexer is done
while (status == IndexerExecutionStatus.InProgress)
{
    status = serviceClient.Indexers.GetStatus(documentIndexer).LastResult.Status;
}

// If successful, delete the flagged files
if (status == IndexerExecutionStatus.Success)
{
    DeleteFlagged();
}

Everything works fine, but only if I put a breakpoint on the DeleteFlagged() call, effectively forcing a delay between running the indexer and deleting the files.

Without the pause, the indexer comes back as successful, and I delete the files, but the file contents haven't been removed from the index - they still show up in search results (the files have been physically deleted).

Is there something else I need to check before deleting?


Solution

  • When you Run an indexer, it doesn't instantly transition into InProgress state - in fact, depending on how many indexers are running in your service, there may be a significant delay before the indexer is scheduled to run. So, when you call GetStatus before the loop, the indexer may not be InProgress yet, and you end up deleting blobs too early. A more reliable approach would be to wait for the indexer to complete this particular run (e.g., by looking at the LastResult's StartTime/EndTime).