| endpoint | delete |
|---|---|
| lang | php |
| es_version | 9.3 |
| client | elasticsearch/elasticsearch==9.3.0 |
Use $client->delete() to remove a document by its ID.
$response = $client->delete(['index' => 'products', 'id' => 'prod-1']);
echo "{$response['result']} document {$response['_id']}\n";A ClientResponseException with status 404 is thrown when the document
does not exist:
use Elastic\Elasticsearch\Exception\ClientResponseException;
try {
$client->delete(['index' => 'products', 'id' => 'prod-999']);
} catch (ClientResponseException $e) {
if ($e->getCode() === 404) {
echo "Document not found — nothing to delete\n";
} else {
throw $e;
}
}Use if_seq_no and if_primary_term for optimistic concurrency
control:
$doc = $client->get(['index' => 'products', 'id' => 'prod-1']);
$client->delete([
'index' => 'products',
'id' => 'prod-1',
'if_seq_no' => $doc['_seq_no'],
'if_primary_term' => $doc['_primary_term'],
]);