Skip to content
API Reference Zilliz Cloud Milvus Attu

Data Operations - Delete

This guide covers deleting data from Milvus collections.

Delete entities by their primary key IDs:

await client.delete({
collection_name: 'my_collection',
ids: [1, 2, 3, 4, 5],
});

For VarChar primary keys:

await client.delete({
collection_name: 'my_collection',
ids: ['id1', 'id2', 'id3'],
});

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"',
});

Use deleteEntities for more control:

await client.deleteEntities({
collection_name: 'my_collection',
expr: 'age < 18',
});

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 entities from a specific partition:

await client.delete({
collection_name: 'my_collection',
partition_name: 'partition_1',
ids: [1, 2, 3],
});

Use the same filter expression syntax as queries:

'age > 25'
'age >= 25'
'age < 25'
'age <= 25'
'age == 25'
'age != 25'
'age > 25 AND category == "tech"'
'age > 25 OR age < 18'
'NOT (age > 25)'
'id in [1, 2, 3, 4, 5]'
'category in ["old", "deprecated"]'
'text like "%deprecated%"'

Delete large numbers of entities efficiently:

// Delete by IDs in batches
const 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 to ensure deletions are persisted:

await client.delete({
collection_name: 'my_collection',
ids: [1, 2, 3],
});
// Flush to persist deletions
await client.flush({
collection_names: ['my_collection'],
});

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);
}
}
  1. Use filters for bulk deletion: Use filter expressions for deleting many entities
  2. Use IDs for specific deletion: Use IDs when deleting specific entities
  3. Flush after deletion: Always flush after deletions to ensure persistence
  4. Batch operations: Delete in batches for large deletions
  5. Verify before delete: Query first to verify what will be deleted
  6. Use transactions: Consider using transactions for related deletions
import { MilvusClient, DataType } from '@zilliz/milvus2-sdk-node';
const client = new MilvusClient({
address: 'localhost:19530',
});
await client.connectPromise;
// Delete by IDs
const deleteResult = await client.delete({
collection_name: 'my_collection',
ids: [1, 2, 3],
});
console.log('Deleted:', deleteResult.delete_cnt);
// Delete by filter
await client.delete({
collection_name: 'my_collection',
expr: 'age < 18',
});
// Flush to persist
await client.flush({
collection_names: ['my_collection'],
});
// Verify deletion
const count = await client.count({
collection_name: 'my_collection',
expr: 'age < 18',
});
console.log('Remaining entities:', count.data);