Is there any simple way to remove all documents (or a filtered list or documents) from an Azure search index?
I know the obvious answer is to delete and recreate the index, but I'm wondering if there is any other option.
Elasticsearch. delete_by_query method can be used for deleting documents matching a query.
Deleting an index deletes its documents, shards, and metadata. It does not delete related Kibana components, such as data views, visualizations, or dashboards.
No, currently there's no way to delete all the documents from an index. As you suspected deleting and re-creating the index is the way to go. For really small indexes you could consider deleting documents individually but given that often apps have code for index creation already, delete/recreate is the quickest path.
There is a way: query all documents, and use IndexBatch to delete these guys.
public void DeleteAllDocuments()
{
// Get index
SearchIndexClient indexClient = new SearchIndexClient(SearchServiceName, SearchServiceIndex, new SearchCredentials(SearchServiceQueryApiKey));
// Query all
DocumentSearchResult<Document> searchResult;
try
{
searchResult = indexClient.Documents.Search<Document>(string.Empty);
}
catch (Exception ex)
{
throw new Exception("Error during AzureSearch");
}
List<string> azureDocsToDelete =
searchResult
.Results
.Select(r => r.Document["id"].ToString())
.ToList();
// Delete all
try
{
IndexBatch batch = IndexBatch.Delete("id", azureDocsToDelete);
var result = indexClient.Documents.Index(batch);
}
catch (IndexBatchException ex)
{
throw new Exception($"Failed to delete documents: {string.Join(", ", ex.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key))}");
}
}
//searchIndex - Index Name
//KeyCol - Key column in the index
public static void ResetIndex(string searchIndex, string KeyCol)
{
while (true)
{
SearchIndexClient indexClient = new SearchIndexClient(searchServiceName, searchIndex, new SearchCredentials(apiKey));
DocumentSearchResult<Document> searchResult = indexClient.Documents.Search<Document>(string.Empty);
List<string> azureDocsToDelete =
searchResult
.Results
.Select(r => r.Document[KeyCol].ToString())
.ToList();
if(azureDocsToDelete.Count != 0)
{
try
{
IndexBatch batch = IndexBatch.Delete(KeyCol, azureDocsToDelete);
var result = indexClient.Documents.Index(batch);
}
catch (IndexBatchException ex)
{
throw new Exception($"Failed to reset the index: {string.Join(", ", ex.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key))}");
}
}
else
{
break;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With