Skip to content

Latest commit

 

History

History
49 lines (39 loc) · 1.08 KB

File metadata and controls

49 lines (39 loc) · 1.08 KB
endpoint delete
lang javascript
es_version 9.3
client @elastic/elasticsearch@9.3.0

Elasticsearch 9.3 delete endpoint (JavaScript example)

Use client.delete() to remove a document by its ID.

const response = await client.delete({ index: "products", id: "prod-1" });
console.log(`${response.result} document ${response._id}`);

Handling missing documents

A ResponseError with status 404 is thrown when the document does not exist:

try {
  await client.delete({ index: "products", id: "prod-999" });
} catch (err) {
  if (err.statusCode === 404) {
    console.log("Document not found — nothing to delete");
  } else {
    throw err;
  }
}

Conditional deletes

Use if_seq_no and if_primary_term for optimistic concurrency control — the delete only proceeds if the document hasn't been modified since you last read it:

const doc = await client.get({ index: "products", id: "prod-1" });

await client.delete({
  index: "products",
  id: "prod-1",
  if_seq_no: doc._seq_no,
  if_primary_term: doc._primary_term,
});