AI Hints
Home›Elasticsearch Tutorial›Create, Update, Delete
Elasticsearch · Lesson 15 of 120

Deleting Documents

And whole indexes.

⏱ 2 min read▶ Video at 13:32🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Products get discontinued, and test data needs cleaning up. You can delete one document by ID, or a whole index at once.

  • Discontinued
  • Clean up
  • Doc or index

Delete a document

DELETE, with the index and ID, removes one document. Then we try to get it. Run it. The result is deleted, and the GET now says found false.

Dev Tools Consoletext
DELETE products/_doc/1?filter_path=result

GET products/_doc/1?filter_path=found
Output
# DELETE products/_doc/1 200 OK
{"result": "deleted"}

# GET products/_doc/1 404 Not Found
{"found": false}
Also uses _setup.http
PUT products
{ "settings": { "number_of_replicas": 0 } }

PUT products/_doc/1?refresh=true
{ "name": "Basecamp 2-Person Tent", "brand": "TrailCo", "price": 237.99, "in_stock": true, "tags": ["camping"] }

Delete an index

DELETE with just an index name removes the index, and every document inside. Run it. Be careful. There's no undo, so in production, protect important indexes.

Dev Tools Consoletext
DELETE products

GET _cat/indices/products
Output
# DELETE products 200 OK
{"acknowledged": true}

# GET _cat/indices/products 404 Not Found
{
  "error": {
    "type": "index_not_found_exception",
    "reason": "no such index [products]"
  },
  "status": 404
}
Also uses _setup.http
PUT products
{ "settings": { "number_of_replicas": 0 } }

PUT products/_doc/1?refresh=true
{ "name": "Basecamp 2-Person Tent", "brand": "TrailCo", "price": 237.99, "in_stock": true, "tags": ["camping"] }

Summary

DELETE_doc removes one document, and DELETE with an index name removes everything in it. Next, versions and conflicts.

Key takeaways
  • DELETE _doc/1
  • DELETE products
  • No undo
Next up: Versions and Conflicts — two writers at once