| endpoint | delete |
|---|---|
| lang | javascript |
| es_version | 9.3 |
| client | @elastic/elasticsearch@9.3.0 |
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}`);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;
}
}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,
});