{"record":{"id":"89552dc31809ce3e","repo":"microsoft/semantic-kernel","slug":"failed-to-remove-all-keys-result-delete-count-r","errorCode":null,"errorMessage":"Failed to remove all keys, {result.delete_count} removed out of {len(keys)}","messagePattern":"Failed to remove all keys, (.+?) removed out of (.+?)","errorType":"exception","errorClass":"ServiceResponseException","httpStatus":null,"severity":"warning","filePath":"python/semantic_kernel/connectors/memory_stores/milvus/milvus_memory_store.py","lineNumber":380,"sourceCode":"        Raises:\n            Exception: Collection doesnt exist.\n            e: Failure to remove key.\n        \"\"\"\n        if collection_name not in utility.list_collections():\n            logger.debug(f\"Collection {collection_name} does not exist, cannot remove.\")\n            raise ServiceResourceNotFoundError(f\"Collection {collection_name} does not exist, cannot remove.\")\n        try:\n            self.collections[collection_name].load()\n            result = self.collections[collection_name].delete(\n                expr=f\"{SEARCH_FIELD_ID} in {keys}\",\n            )\n            self.collections[collection_name].flush()\n        except Exception as e:\n            logger.debug(f\"Remove failed due to: {e}\")\n            raise ServiceResponseException(f\"Remove failed due to: {e}\") from e\n        if result.delete_count != len(keys):\n            logger.debug(f\"Failed to remove all keys, {result.delete_count} removed out of {len(keys)}\")\n            raise ServiceResponseException(\n                f\"Failed to remove all keys, {result.delete_count} removed out of {len(keys)}\"\n            )\n\n    async def get_nearest_matches(\n        self,\n        collection_name: str,\n        embedding: ndarray,\n        limit: int,\n        min_relevance_score: float = 0.0,\n        with_embeddings: bool = False,\n    ) -> list[tuple[MemoryRecord, float]]:\n        \"\"\"Find the nearest `limit` matches for an embedding.\n\n        Args:\n            collection_name (str): The collection to search.\n            embedding (ndarray): The embedding to search.\n            limit (int): The total results to display.\n            min_relevance_score (float, optional): Minimum distance to include. Defaults to None.","sourceCodeStart":362,"sourceCodeEnd":398,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/memory_stores/milvus/milvus_memory_store.py#L362-L398","documentation":"Raised as a ServiceResponseException in MilvusMemoryStore.remove_batch when result.delete_count does not equal len(keys), meaning some keys were not deleted (typically because they did not exist in the collection). This is a partial-success signal: the delete operation completed but not all requested keys matched.","triggerScenarios":"Calling remove_batch with a keys list where some keys are absent from the collection. Milvus returns a delete_count lower than the number of keys submitted, so the mismatch check triggers even though the operation itself succeeded.","commonSituations":"Idempotent cleanup passing already-deleted or never-existing keys. Stale key references from a different data run. Bulk deletion where a subset of records were removed by a prior operation.","solutions":["If partial deletion is acceptable, catch ServiceResponseException and log which keys may remain.","Pre-filter keys to those known to exist (via a query) before calling remove_batch.","Treat remove as idempotent: ignore this error if the end state (keys absent) is the goal.","Log result.delete_count vs len(keys) for diagnostics."],"exampleFix":"// before\nawait store.remove_batch('docs', keys)  # ServiceResponseException: Failed to remove all keys...\n// after\ntry:\n    await store.remove_batch('docs', keys)\nexcept ServiceResponseException as e:\n    if 'Failed to remove all keys' in str(e):\n        logging.info('Partial remove (some keys already absent): %s', e)\n    else:\n        raise","handlingStrategy":"try-catch","validationCode":"# Pre-filter keys to those that exist before removing\nexisting = await store.get_batch('docs', keys, with_embeddings=False)\nexisting_ids = {r._id for r in existing}\nkeys_to_remove = [k for k in keys if k in existing_ids]\nif keys_to_remove:\n    await store.remove_batch('docs', keys_to_remove)","typeGuard":null,"tryCatchPattern":"from semantic_kernel.exceptions import ServiceResponseException\n\ntry:\n    await store.remove_batch('docs', keys)\nexcept ServiceResponseException as e:\n    if 'Failed to remove all keys' in str(e):\n        logging.info('Partial remove — some keys already absent: %s', e)\n    else:\n        raise","preventionTips":["Treat remove_batch as idempotent: some missing keys is often acceptable.","Pre-filter keys to existing records when exact counts matter.","Log delete_count vs len(keys) for diagnostics.","Catch the partial-failure message distinctly from hard failures."],"tags":["milvus","remove","partial-failure","idempotency","python"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}