Data Operations - Delete
This guide covers deleting data from Milvus collections.
Delete by IDs
Section titled “Delete by IDs”Delete entities by their primary key IDs:
await client.delete({ collection_name: 'my_collection', ids: [1, 2, 3, 4, 5],});Delete with String IDs
Section titled “Delete with String IDs”For VarChar primary keys:
await client.delete({ collection_name: 'my_collection', ids: ['id1', 'id2', 'id3'],});Delete by Filter Expression
Section titled “Delete by Filter Expression”Delete entities matching a filter expression:
await client.delete({ collection_name: 'my_collection', filter: 'age < 18',});Or using expr:
await client.delete({ collection_name: 'my_collection', expr: 'age < 18 AND category == "old"',});Delete Entities (Advanced)
Section titled “Delete Entities (Advanced)”Use deleteEntities for more control:
await client.deleteEntities({ collection_name: 'my_collection', expr: 'age < 18',});Delete Response
Section titled “Delete Response”Delete operations return mutation results:
const result = await client.delete({ collection_name: 'my_collection', ids: [1, 2, 3],});
console.log('Delete count:', result.delete_cnt);console.log('IDs:', result.IDs);console.log('Timestamp:', result.timestamp);Delete from Specific Partition
Section titled “Delete from Specific Partition”Delete entities from a specific partition:
await client.delete({ collection_name: 'my_collection', partition_name: 'partition_1', ids: [1, 2, 3],});Filter Expressions for Delete
Section titled “Filter Expressions for Delete”Use the same filter expression syntax as queries:
Comparison Operators
Section titled “Comparison Operators”'age > 25''age >= 25''age < 25''age <= 25''age == 25''age != 25'Logical Operators
Section titled “Logical Operators”'age > 25 AND category == "tech"''age > 25 OR age < 18''NOT (age > 25)'In Operator
Section titled “In Operator”'id in [1, 2, 3, 4, 5]''category in ["old", "deprecated"]'String Operations
Section titled “String Operations”'text like "%deprecated%"'Batch Deletion
Section titled “Batch Deletion”Delete large numbers of entities efficiently:
// Delete by IDs in batchesconst idsToDelete = [/* large array of IDs */];const batchSize = 1000;
for (let i = 0; i < idsToDelete.length; i += batchSize) { const batch = idsToDelete.slice(i, i + batchSize); await client.delete({ collection_name: 'my_collection', ids: batch, });}Flush After Delete
Section titled “Flush After Delete”Flush to ensure deletions are persisted:
await client.delete({ collection_name: 'my_collection', ids: [1, 2, 3],});
// Flush to persist deletionsawait client.flush({ collection_names: ['my_collection'],});Error Handling
Section titled “Error Handling”Handle deletion errors:
try { await client.delete({ collection_name: 'my_collection', ids: [1, 2, 3], });} catch (error) { if (error.message.includes('CollectionNotExists')) { console.error('Collection does not exist'); } else if (error.message.includes('IllegalArgument')) { console.error('Invalid delete parameters'); } else { console.error('Delete failed:', error); }}Delete Best Practices
Section titled “Delete Best Practices”- Use filters for bulk deletion: Use filter expressions for deleting many entities
- Use IDs for specific deletion: Use IDs when deleting specific entities
- Flush after deletion: Always flush after deletions to ensure persistence
- Batch operations: Delete in batches for large deletions
- Verify before delete: Query first to verify what will be deleted
- Use transactions: Consider using transactions for related deletions
Complete Example
Section titled “Complete Example”import { MilvusClient, DataType } from '@zilliz/milvus2-sdk-node';
const client = new MilvusClient({ address: 'localhost:19530',});
await client.connectPromise;
// Delete by IDsconst deleteResult = await client.delete({ collection_name: 'my_collection', ids: [1, 2, 3],});
console.log('Deleted:', deleteResult.delete_cnt);
// Delete by filterawait client.delete({ collection_name: 'my_collection', expr: 'age < 18',});
// Flush to persistawait client.flush({ collection_names: ['my_collection'],});
// Verify deletionconst count = await client.count({ collection_name: 'my_collection', expr: 'age < 18',});
console.log('Remaining entities:', count.data);Next Steps
Section titled “Next Steps”- Learn about Data Management
- Explore Query Operations
- Check out Best Practices