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
- Ruby
- Scala
- Swift
using Algolia.Search.Clients;
using Algolia.Search.Http;
var client = new SearchClient(new SearchConfig("YOUR_APP_ID", "YOUR_API_KEY"));
var response = await client.BatchAsync(
"<YOUR_INDEX_NAME>",
new BatchWriteParams
{
Requests = new List<BatchRequest>
{
new BatchRequest
{
Action = Enum.Parse<Action>("DeleteObject"),
Body = new Dictionary<string, string> { { "key", "value" } },
}
},
}
);
await client.WaitForTaskAsync("<YOUR_INDEX_NAME>", response.TaskID);
import 'package:algolia_client_search/algolia_client_search.dart';
final client = SearchClient(appId: 'YOUR_APP_ID', apiKey: 'YOUR_API_KEY');
final response = await client.batch(
indexName: "<YOUR_INDEX_NAME>",
batchWriteParams: BatchWriteParams(
requests: [
BatchRequest(
action: Action.fromJson("deleteObject"),
body: {
'key': "value",
},
),
],
),
);
await client.waitTask('<YOUR_INDEX_NAME>', response.taskID);
import "github.com/algolia/algoliasearch-client-go/v4/algolia/search"
client, err := search.NewClient("YOUR_APP_ID", "YOUR_API_KEY")
response, err := client.Batch(client.NewApiBatchRequest(
"<YOUR_INDEX_NAME>",
search.NewEmptyBatchWriteParams().SetRequests(
[]search.BatchRequest{*search.NewEmptyBatchRequest().SetAction(search.Action("deleteObject")).SetBody(map[string]any{"key": "value"})}),
))
if err != nil {
// handle the eventual error
panic(err)
}
// use the model directly
print(response)
taskResponse, err := searchClient.WaitForTask("<YOUR_INDEX_NAME>", response.TaskID, nil, nil, nil)
if err != nil {
panic(err)
}
import com.algolia.api.SearchClient;
import com.algolia.model.search.*;
SearchClient client = new SearchClient("YOUR_APP_ID", "YOUR_API_KEY");
client.batch(
"<YOUR_INDEX_NAME>",
new BatchWriteParams().setRequests(List.of(new BatchRequest().setAction(Action.DELETE_OBJECT).setBody(Map.of("key", "value"))))
);
client.waitForTask("<YOUR_INDEX_NAME>", response.getTaskID());
import { searchClient } from '@algolia/client-search';
const client = searchClient('YOUR_APP_ID', 'YOUR_API_KEY');
const response = await client.batch({
indexName: '<YOUR_INDEX_NAME>',
batchWriteParams: {
requests: [{ action: 'deleteObject', body: { key: 'value' } }],
},
});
// use typed response
console.log(response);
await client.waitForTask({ indexName: '<YOUR_INDEX_NAME>', taskID: response.taskID });
import com.algolia.client.api.SearchClient
val client = SearchClient(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY")
var response = client.batch(
indexName = "<YOUR_INDEX_NAME>",
batchWriteParams = BatchWriteParams(
requests = listOf(
BatchRequest(
action = Action.entries.first { it.value == "deleteObject" },
body = buildJsonObject {
put(
"key",
JsonPrimitive("value"),
)
},
),
),
),
)
// Use the response
println(response)
client.waitTask("<YOUR_INDEX_NAME>", response.taskID)
use Algolia\AlgoliaSearch\Api\SearchClient;
$client = SearchClient::create('<YOUR_APP_ID>', '<YOUR_API_KEY>');
$response = $client->batch(
'<YOUR_INDEX_NAME>',
['requests' => [
['action' => 'deleteObject',
'body' => ['key' => 'value',
],
],
],
],
);
// play with the response
var_dump($response);
$client->waitForTask('<YOUR_INDEX_NAME>', $response['taskID']);
from algoliasearch.search.client import SearchClient
_client = SearchClient("YOUR_APP_ID", "YOUR_API_KEY")
response = await _client.batch(
index_name="<YOUR_INDEX_NAME>",
batch_write_params={
"requests": [
{
"action": "deleteObject",
"body": {
"key": "value",
},
},
],
},
)
# use the class directly
print(response)
# print the JSON response
print(response.to_json())
await client.wait_for_task(index_name="<YOUR_INDEX_NAME>", task_id=response.task_id)
require 'algolia'
client = Algolia::SearchClient.create('YOUR_APP_ID', 'YOUR_API_KEY')
response = client.batch(
"<YOUR_INDEX_NAME>",
BatchWriteParams.new(
requests: [BatchRequest.new(
action: 'deleteObject',
body: { key: "value" }
)]
)
)
# use the class directly
puts response
# print the JSON response
puts response.to_json
client.wait_for_task("<YOUR_INDEX_NAME>", response.task_id)
import algoliasearch.api.SearchClient
val client = SearchClient(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY")
val response = client.batch(
indexName = "<YOUR_INDEX_NAME>",
batchWriteParams = BatchWriteParams(
requests = Seq(
BatchRequest(
action = Action.withName("deleteObject"),
body = JObject(List(JField("key", JString("value"))))
)
)
)
)
// Use the response
val value = Await.result(response, Duration(100, "sec"))
client.waitTask("<YOUR_INDEX_NAME>", response.getTaskID())
import Search
let client = try SearchClient(appID: "YOUR_APP_ID", apiKey: "YOUR_API_KEY")
let response = try await client.batch(
indexName: "<YOUR_INDEX_NAME>",
batchWriteParams: BatchWriteParams(requests: [BatchRequest(
action: Action.deleteObject,
body: ["key": "value"]
)])
)
try await client.waitForTask(with: response.taskID, in: "<YOUR_INDEX_NAME>")
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
.