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.
- C#
- Dart
- Go
- Java
- JavaScript
- Kotlin
- PHP
- Python
- Scala
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
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);
val objectIDs = listOf("1", "2", "3", "4", "5")
client.batch(
indexName = "<YOUR_INDEX_NAME>",
batchWriteParams = BatchWriteParams(
requests = objectIDs.map { objectID ->
BatchRequest(
action = Action.Delete,
body = buildJsonObject {
put("objectID", JsonPrimitive(objectID))
},
)
}
)
)
var objectIDs = ['1', '2', '3', '4', '5'];
await client.batch(
indexName: '<YOUR_INDEX_NAME>',
batchWriteParams: BatchWriteParams(
requests: objectIDs
.map((id) => BatchRequest(
action: Action.delete,
body: {'objectID': id},
))
.toList(),
),
);
import (
"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
)
indexName := "<INDEX_NAME>"
appID := "<APPLICATION_ID>"
apiKey := "<API_KEY>"
searchClient, _ := search.NewClient(appID, apiKey)
objectIDs := []string{"1", "2", "3", "4", "5"}
batchRequests := make([]search.BatchRequest, 0, len(objectIDs))
for _, objectID := range objectIDs {
batchRequests = append(
batchRequests,
*search.NewBatchRequest(search.ACTION_DELETE, map[string]any{"objectID": objectID}),
)
}
batchWriteParams := search.NewBatchWriteParams(batchRequests)
batchResponse, err := searchClient.Batch(searchClient.NewApiBatchRequest(
indexName,
batchWriteParams,
))
if err != nil {
panic(err)
}
taskResponse, err := searchClient.WaitForTask(
indexName,
batchResponse.TaskID,
nil,
nil,
nil,
)
if err != nil {
panic(err)
}
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
.