milvus-logo
LFAI
Home
  • User Guide

Delete Entities

This topic describes how to delete entities in Milvus.

Milvus supports deleting entities by primary key or complex boolean expressions. Deleting entities by primary key is much faster and lighter than deleting them by complex boolean expressions. This is because Milvus executes queries first when deleting data by complex boolean expressions.

  • Deleted entities can still be retrieved immediately after the deletion if the consistency level is set lower than Strong.
  • Entities deleted beyond the pre-specified span of time for Time Travel cannot be retrieved again.
  • Frequent deletion operations will impact the system performance.
  • Before deleting entities by comlpex boolean expressions, make sure the collection has been loaded.
  • Deleting entities by complex boolean expressions is not an atomic operation. Therefore, if it fails halfway through, some data may still be deleted.
  • Deleting entities by complex boolean expressions is supported only when the consistency is set to Bounded. For details, see Consistency.

When interacting with Milvus using Python code, you have the flexibility to choose between PyMilvus and MilvusClient (new). For more information, refer to Python SDK.

Prepare boolean expression

Prepare the boolean expression that filters the entities to delete.

Milvus supports deleting entities by primary key or complex boolean expressions. For more information on expression rules and supported operators, see Boolean Expression Rules.

Simple boolean expression

Use a simple expression to filter data with primary key values of 0 and 1:

expr = "book_id in [0,1]"
const expr = "book_id in [0,1]";
expr := "book_id in [0,1]"
private static final String DELETE_EXPR = "book_id in [0,1]";
delete entities -c book
The expression to specify entities to be deleted: book_id in [0,1]
# See the following section.
Option Description
-c The name of the collection.
-p (Optional) The name of the partition that the entities belong to.

Complex boolean expression

To filter entities that meet specific conditions, define complex boolean expressions.

Filter entities whose word_count is greater than or equal to 11000:

expr = "word_count >= 11000"
expr := "word_count >= 11000"

Filter entities whose book_name is not Unknown:

expr = "book_name != Unknown"
expr := "book_name != Unknown"

Filter entities whose primary key values are greater than 5 and word_count is smaller than or equal to 9999:

expr = "book_id > 5 && word_count <= 9999"
expr := "book_id > 5 && word_count <= 9999"

Delete entities

Delete the entities with the boolean expression you created. Milvus returns the ID list of the deleted entities.

from pymilvus import Collection
collection = Collection("book")      # Get an existing collection.
collection.delete(expr)
await milvusClient.deleteEntities({
  collection_name: "book",
  expr: expr,
});
err = milvusClient.Delete(
    context.Background(), // ctx
    "book",               // collection name
    "",                   // partition name
    expr,                 // expr
)
if err != nil {
    log.Fatal("failed to delete:", err.Error())
}
milvusClient.delete(
  DeleteParam.newBuilder()
    .withCollectionName("book")
    .withExpr(DELETE_EXPR)
    .build()
);
You are trying to delete the entities of collection. This action cannot be undone!
Do you want to continue? [y/N]: y
curl -X 'POST' \
  '${MILVUS_HOST}:${MILVUS_PORT}/v1/vector/delete' \
  -H 'Authorization: Bearer ${TOKEN}' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
       "collectionName": "collection1",
       "id": 1
     }'
Output:
{
    "code": 200,
    "data": {}
}
Parameter Description
expr Boolean expression that specifies the entities to delete.
partition_name (optional) Name of the partition to delete entities from.
Parameter Description
collection_name Name of the collection to delete entities from.
expr Boolean expression that specifies the entities to delete.
partition_name (optional) Name of the partition to delete entities from.
Parameter Description
CollectionName Name of the collection to delete entities from.
expr Boolean expression that specifies the entities to delete.
PartitionName (optional) Name of the partition to delete entities from.
Parameter Description
collectionName The name of the collection to which this operation applies.
id The ID of the entity to drop.

What's next

Feedback

Was this page helpful?