How to delete specific documents from a search index ?

Aravind Vijayaraghavan 40 Reputation points
2025-01-22T10:42:29.4966667+00:00

I tried to delete some few documents beyond a certain date for my date field, but it ended up deleting a lot more. I realised its because my date field is just string values so it ended up in so much deletion. How do I delete specific documents for greater than or lesser than values for certain fields or for string fields specifically? All my fields are strings and vectors. This is my current code:

def chunk_data(data, chunk_size):
    """Helper function to chunk data into smaller pieces."""
    for i in range(0, len(data), chunk_size):
        yield data[i:i + chunk_size]


def delete_documents_from_search_client(date_ymd, chunk_size=32000):
    """Deletes documents from the search index with 'date_ymd' equal or below the specified date."""
    query = f"date_ymd le '{date_ymd}'"
    results = search_client.search(query)  
    
    data_to_delete = []

    for result in results:
        document_id = result["hardware_id"]
        data_to_delete.append({
            "@search.action": "delete",
            "hardware_id": document_id
        })

    for chunk in chunk_data(data_to_delete, chunk_size):
        try:
            result = search_client.upload_documents(documents=chunk)
            print(f"Deleted {len(chunk)} documents successfully.")
        except HttpResponseError as e:
            print(f"An error occurred during document delete: {e}")
            return None

delete_documents_from_search_client("2024-12-02")
Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
1,194 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.