Skip to content

Latest commit

 

History

History
50 lines (39 loc) · 1.09 KB

File metadata and controls

50 lines (39 loc) · 1.09 KB
endpoint delete
lang php
es_version 9.3
client elasticsearch/elasticsearch==9.3.0

Elasticsearch 9.3 delete endpoint (PHP example)

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";

Handling missing documents

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;
    }
}

Conditional deletes

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'],
]);