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

How to delete a document (table) or a group of documents using Azure Search SDK (Library)


I want to delete an item (document) using the document Id using the library Microsoft.Azure.Search. How can I do that?

Follows what I've tried so far:

public Task DeleteItems(IEnumerable<string> itemsIds)
        {
            return Task.Run(() =>
            {
                IndexBatch<string> batch = IndexBatch.Delete<string>(itemsIds);
                try
                {
                     //Gets the search service and add the delete batch to be perfomed on the Index
                    this.GetSearchServiceIndex().Documents.Index(batch);
                }
                catch (IndexBatchException ex)
                {
                    //Do something in here
                }
            });
        }

Solution

  • I found out this git post I adapted to my case. The final result sounds like this:

    public Task DeleteItems(IEnumerable<string> itemsIds)
        {
            return Task.Run(() =>
            {
                IndexBatch batch = IndexBatch.Delete("id", itemsIds);
                try
                {
                    //Gets the search service and add the delete batch to be perfomed on the Index
                    this.GetSearchServiceIndex().Documents.Index(batch);
                }
                catch (IndexBatchException ex)
                {
                    //Do Someting here
                }
            });
        }