| endpoint | delete |
|---|---|
| lang | python |
| es_version | 9.3 |
| client | elasticsearch==9.3.0 |
Use client.delete() to remove a document by its ID.
response = client.delete(index="products", id="prod-1")
print(f"{response['result']} document {response['_id']}")A NotFoundError is raised when the document does not exist:
from elasticsearch import NotFoundError
try:
client.delete(index="products", id="prod-999")
except NotFoundError:
print("Document not found — nothing to delete")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:
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"],
)