# Delete Entities

Delete Milvus entities by primary key or filter expression with the Node.js SDK and check the deletion response.

SDK: `@zilliz/milvus2-sdk-node@3.0.10` · Tested with Milvus 3.0.6

Canonical documentation: https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/

## Section links

- [Delete by IDs](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#delete-by-ids)
- [Delete with String IDs](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#delete-with-string-ids)
- [Delete by Filter Expression](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#delete-by-filter-expression)
- [Delete Entities (Advanced)](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#delete-entities-advanced)
- [Delete Response](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#delete-response)
- [Delete from Specific Partition](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#delete-from-specific-partition)
- [Filter Expressions for Delete](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#filter-expressions-for-delete)
- [Comparison Operators](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#comparison-operators)
- [Logical Operators](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#logical-operators)
- [In Operator](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#in-operator)
- [String Operations](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#string-operations)
- [Batch Deletion](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#batch-deletion)
- [Flush After Delete](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#flush-after-delete)
- [Error Handling](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#error-handling)
- [Delete Best Practices](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#delete-best-practices)
- [Complete Example](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#complete-example)
- [Next Steps](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-delete/#next-steps)

This guide covers deleting data from Milvus collections.

## Delete by IDs

Delete entities by their primary key IDs:

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

### Delete with String IDs

For VarChar primary keys:

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

## Delete by Filter Expression

Delete entities matching a filter expression:

```javascript
await client.delete({
  collection_name: 'my_collection',
  filter: 'age < 18',
});
```

Combine conditions with `AND`:

```javascript
await client.delete({
  collection_name: 'my_collection',
  filter: 'age < 18 AND category == "old"',
});
```

## Delete Entities (Advanced)

Use `deleteEntities` for more control:

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

## Delete Response

Delete operations return mutation results:

```javascript
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

Delete entities from a specific partition:

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

## Filter Expressions for Delete

Use the same filter expression syntax as queries:

### Comparison Operators

```javascript
'age > 25';
'age >= 25';
'age < 25';
'age <= 25';
'age == 25';
'age != 25';
```

### Logical Operators

```javascript
'age > 25 AND category == "tech"';
'age > 25 OR age < 18';
'NOT (age > 25)';
```

### In Operator

```javascript
'id in [1, 2, 3, 4, 5]';
'category in ["old", "deprecated"]';
```

### String Operations

```javascript
'text like "%deprecated%"';
```

## Batch Deletion

Delete large numbers of entities efficiently:

```javascript
// 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 After Delete

Flush to ensure deletions are persisted:

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

// Flush to persist deletions
await client.flush({
  collection_names: ['my_collection'],
});
```

## Error Handling

Handle deletion errors:

```javascript
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

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

## Complete Example

Use a collection named `my_collection` with an Int64 primary key and an integer `age` field. Load it before using `count()` to verify the deletion.

```javascript
import { ConsistencyLevelEnum, MilvusClient } 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',
  filter: 'age < 18',
});

// Flush to persist
await client.flush({
  collection_names: ['my_collection'],
});

// Verify deletion
const count = await client.count({
  collection_name: 'my_collection',
  filter: 'age < 18',
  consistency_level: ConsistencyLevelEnum.Strong,
});

console.log('Remaining entities:', count.data);
await client.closeConnection();
```

## Next Steps

- Learn about [Data Management](https://milvus-io.github.io/milvus-sdk-node/operations/data-management/)
- Explore [Query Operations](https://milvus-io.github.io/milvus-sdk-node/operations/data-operations-query/)
- Check out [Best Practices](https://milvus-io.github.io/milvus-sdk-node/advanced/best-practices/)
