Delete Multiple Objects using Batch
In Algolia, you can delete multiple objects efficiently using the batch functionality. This guide will show you how to delete multiple objects at once.
Delete Multiple Objects
To delete multiple objects using batch, create a batch write params object that contains the array of delete requests.
Each delete request should have the action
set to deleteObject
and the body
containing the respective object ID.
This allows you to define multiple delete operations within the requests
array.
- JavaScript
- PHP
- Java
javascript
const objectIDs = ['1', '2', '3', '4', '5'];const batchWriteParams = {requests: objectIDs.map(id => ({action: 'deleteObject',body: {objectID: id,},})),};await client.batch({ indexName, batchWriteParams });
javascript
const objectIDs = ['1', '2', '3', '4', '5'];const batchWriteParams = {requests: objectIDs.map(id => ({action: 'deleteObject',body: {objectID: id,},})),};await client.batch({ indexName, batchWriteParams });
php
// TODO
java
List<String> objectIDs = Arrays.asList("1", "2", "3", "4", "5");BatchWriteParams batchWriteParams = new BatchWriteParams();for (String objectID : objectIDs) {batchWriteParams.addRequests(new BatchRequest().setAction(Action.DELETE).setBody(Collections.singletonMap("objectID", objectID)));}client.batch(indexName, batchWriteParams);
java
List<String> objectIDs = Arrays.asList("1", "2", "3", "4", "5");BatchWriteParams batchWriteParams = new BatchWriteParams();for (String objectID : objectIDs) {batchWriteParams.addRequests(new BatchRequest().setAction(Action.DELETE).setBody(Collections.singletonMap("objectID", objectID)));}client.batch(indexName, batchWriteParams);
By executing the above code, all the objects with the specified object IDs will be deleted from your index.
That's it! You have successfully deleted multiple objects using batch
.