| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 | [[docs-delete]]=== Delete API++++<titleabbrev>Delete</titleabbrev>++++Removes a JSON document from the specified index.[[docs-delete-api-request]]==== {api-request-title}`DELETE /<index>/_doc/<_id>`[[docs-delete-api-desc]]==== {api-description-title}You use DELETE to remove a document from an index. You must specify theindex name and document ID.[float][[optimistic-concurrency-control-delete]]===== Optimistic concurrency controlDelete operations can be made conditional and only be performed if the lastmodification to the document was assigned the sequence number and primaryterm specified by the `if_seq_no` and `if_primary_term` parameters. If amismatch is detected, the operation will result in a `VersionConflictException`and a status code of 409. See <<optimistic-concurrency-control>> for more details.[float][[delete-versioning]]===== VersioningEach document indexed is versioned. When deleting a document, the `version` canbe specified to make sure the relevant document we are trying to delete isactually being deleted and it has not changed in the meantime. Every writeoperation executed on a document, deletes included, causes its version to beincremented. The version number of a deleted document remains available for ashort time after deletion to allow for control of concurrent operations. Thelength of time for which a deleted document's version remains available isdetermined by the `index.gc_deletes` index setting and defaults to 60 seconds.[float][[delete-routing]]===== RoutingIf routing is used during indexing, the routing value also needs to bespecified to delete a document.If the `_routing` mapping is set to `required` and no routing value isspecified, the delete API throws a `RoutingMissingException` and rejectsthe request.For example:////Example to delete with routing[source,console]--------------------------------------------------PUT /twitter/_doc/1?routing=kimchy{    "test": "test"}--------------------------------------------------////[source,console]--------------------------------------------------DELETE /twitter/_doc/1?routing=kimchy--------------------------------------------------// TEST[continued]This request deletes the tweet with id `1`, but it is routed based on theuser. The document is not deleted if the correct routing is not specified.[float][[delete-index-creation]]===== Automatic index creationIf an <<docs-index_,external versioning variant>> is used,the delete operation automatically creates the specified index if it does notexist. For information about manually creating indices, see<<indices-create-index,create index API>>.[float][[delete-distributed]]===== DistributedThe delete operation gets hashed into a specific shard id. It then getsredirected into the primary shard within that id group, and replicated(if needed) to shard replicas within that id group.[float][[delete-wait-for-active-shards]]===== Wait for active shardsWhen making delete requests, you can set the `wait_for_active_shards`parameter to require a minimum number of shard copies to be activebefore starting to process the delete request. See<<index-wait-for-active-shards,here>> for further details and a usageexample.[float][[delete-refresh]]===== RefreshControl when the changes made by this request are visible to search. See<<docs-refresh>>.[float][[delete-timeout]]===== TimeoutThe primary shard assigned to perform the delete operation might not beavailable when the delete operation is executed. Some reasons for thismight be that the primary shard is currently recovering from a storeor undergoing relocation. By default, the delete operation will wait onthe primary shard to become available for up to 1 minute before failingand responding with an error. The `timeout` parameter can be used toexplicitly specify how long it waits. Here is an example of setting itto 5 minutes:[source,console]--------------------------------------------------DELETE /twitter/_doc/1?timeout=5m--------------------------------------------------// TEST[setup:twitter][[docs-delete-api-path-params]]==== {api-path-parms-title}`<index>`::(Required, string) Name of the target index.`<_id>`::(Required, string) Unique identifier for the document.[[docs-delete-api-query-params]]==== {api-query-parms-title}include::{docdir}/rest-api/common-parms.asciidoc[tag=if_seq_no]include::{docdir}/rest-api/common-parms.asciidoc[tag=if_primary_term]include::{docdir}/rest-api/common-parms.asciidoc[tag=pipeline]include::{docdir}/rest-api/common-parms.asciidoc[tag=refresh]include::{docdir}/rest-api/common-parms.asciidoc[tag=routing]include::{docdir}/rest-api/common-parms.asciidoc[tag=timeout]include::{docdir}/rest-api/common-parms.asciidoc[tag=doc-version]include::{docdir}/rest-api/common-parms.asciidoc[tag=version_type]include::{docdir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards][[docs-delete-api-example]]==== {api-examples-title}Delete the JSON document `1` from the `twitter` index:[source,console]--------------------------------------------------DELETE /twitter/_doc/1--------------------------------------------------// TEST[setup:twitter]The API returns the following result:[source,console-result]--------------------------------------------------{    "_shards" : {        "total" : 2,        "failed" : 0,        "successful" : 2    },    "_index" : "twitter",    "_id" : "1",    "_version" : 2,    "_primary_term": 1,    "_seq_no": 5,    "result": "deleted"}--------------------------------------------------// TESTRESPONSE[s/"successful" : 2/"successful" : 1/]// TESTRESPONSE[s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]// TESTRESPONSE[s/"_seq_no" : 5/"_seq_no" : $body._seq_no/]
 |