delete.asciidoc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. [[docs-delete]]
  2. === Delete API
  3. ++++
  4. <titleabbrev>Delete</titleabbrev>
  5. ++++
  6. Removes a JSON document from the specified index.
  7. [[docs-delete-api-request]]
  8. ==== {api-request-title}
  9. `DELETE /<index>/_doc/<_id>`
  10. [[docs-delete-api-desc]]
  11. ==== {api-description-title}
  12. You use DELETE to remove a document from an index. You must specify the
  13. index name and document ID.
  14. [float]
  15. [[optimistic-concurrency-control-delete]]
  16. ===== Optimistic concurrency control
  17. Delete operations can be made conditional and only be performed if the last
  18. modification to the document was assigned the sequence number and primary
  19. term specified by the `if_seq_no` and `if_primary_term` parameters. If a
  20. mismatch is detected, the operation will result in a `VersionConflictException`
  21. and a status code of 409. See <<optimistic-concurrency-control>> for more details.
  22. [float]
  23. [[delete-versioning]]
  24. ===== Versioning
  25. Each document indexed is versioned. When deleting a document, the `version` can
  26. be specified to make sure the relevant document we are trying to delete is
  27. actually being deleted and it has not changed in the meantime. Every write
  28. operation executed on a document, deletes included, causes its version to be
  29. incremented. The version number of a deleted document remains available for a
  30. short time after deletion to allow for control of concurrent operations. The
  31. length of time for which a deleted document's version remains available is
  32. determined by the `index.gc_deletes` index setting and defaults to 60 seconds.
  33. [float]
  34. [[delete-routing]]
  35. ===== Routing
  36. If routing is used during indexing, the routing value also needs to be
  37. specified to delete a document.
  38. If the `_routing` mapping is set to `required` and no routing value is
  39. specified, the delete API throws a `RoutingMissingException` and rejects
  40. the request.
  41. For example:
  42. ////
  43. Example to delete with routing
  44. [source,console]
  45. --------------------------------------------------
  46. PUT /twitter/_doc/1?routing=kimchy
  47. {
  48. "test": "test"
  49. }
  50. --------------------------------------------------
  51. ////
  52. [source,console]
  53. --------------------------------------------------
  54. DELETE /twitter/_doc/1?routing=kimchy
  55. --------------------------------------------------
  56. // TEST[continued]
  57. This request deletes the tweet with id `1`, but it is routed based on the
  58. user. The document is not deleted if the correct routing is not specified.
  59. [float]
  60. [[delete-index-creation]]
  61. ===== Automatic index creation
  62. If an <<docs-index_,external versioning variant>> is used,
  63. the delete operation automatically creates the specified index if it does not
  64. exist. For information about manually creating indices, see
  65. <<indices-create-index,create index API>>.
  66. [float]
  67. [[delete-distributed]]
  68. ===== Distributed
  69. The delete operation gets hashed into a specific shard id. It then gets
  70. redirected into the primary shard within that id group, and replicated
  71. (if needed) to shard replicas within that id group.
  72. [float]
  73. [[delete-wait-for-active-shards]]
  74. ===== Wait for active shards
  75. When making delete requests, you can set the `wait_for_active_shards`
  76. parameter to require a minimum number of shard copies to be active
  77. before starting to process the delete request. See
  78. <<index-wait-for-active-shards,here>> for further details and a usage
  79. example.
  80. [float]
  81. [[delete-refresh]]
  82. ===== Refresh
  83. Control when the changes made by this request are visible to search. See
  84. <<docs-refresh>>.
  85. [float]
  86. [[delete-timeout]]
  87. ===== Timeout
  88. The primary shard assigned to perform the delete operation might not be
  89. available when the delete operation is executed. Some reasons for this
  90. might be that the primary shard is currently recovering from a store
  91. or undergoing relocation. By default, the delete operation will wait on
  92. the primary shard to become available for up to 1 minute before failing
  93. and responding with an error. The `timeout` parameter can be used to
  94. explicitly specify how long it waits. Here is an example of setting it
  95. to 5 minutes:
  96. [source,console]
  97. --------------------------------------------------
  98. DELETE /twitter/_doc/1?timeout=5m
  99. --------------------------------------------------
  100. // TEST[setup:twitter]
  101. [[docs-delete-api-path-params]]
  102. ==== {api-path-parms-title}
  103. `<index>`::
  104. (Required, string) Name of the target index.
  105. `<_id>`::
  106. (Required, string) Unique identifier for the document.
  107. [[docs-delete-api-query-params]]
  108. ==== {api-query-parms-title}
  109. include::{docdir}/rest-api/common-parms.asciidoc[tag=if_seq_no]
  110. include::{docdir}/rest-api/common-parms.asciidoc[tag=if_primary_term]
  111. include::{docdir}/rest-api/common-parms.asciidoc[tag=pipeline]
  112. include::{docdir}/rest-api/common-parms.asciidoc[tag=refresh]
  113. include::{docdir}/rest-api/common-parms.asciidoc[tag=routing]
  114. include::{docdir}/rest-api/common-parms.asciidoc[tag=timeout]
  115. include::{docdir}/rest-api/common-parms.asciidoc[tag=doc-version]
  116. include::{docdir}/rest-api/common-parms.asciidoc[tag=version_type]
  117. include::{docdir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  118. [[docs-delete-api-example]]
  119. ==== {api-examples-title}
  120. Delete the JSON document `1` from the `twitter` index:
  121. [source,console]
  122. --------------------------------------------------
  123. DELETE /twitter/_doc/1
  124. --------------------------------------------------
  125. // TEST[setup:twitter]
  126. The API returns the following result:
  127. [source,console-result]
  128. --------------------------------------------------
  129. {
  130. "_shards" : {
  131. "total" : 2,
  132. "failed" : 0,
  133. "successful" : 2
  134. },
  135. "_index" : "twitter",
  136. "_id" : "1",
  137. "_version" : 2,
  138. "_primary_term": 1,
  139. "_seq_no": 5,
  140. "result": "deleted"
  141. }
  142. --------------------------------------------------
  143. // TESTRESPONSE[s/"successful" : 2/"successful" : 1/]
  144. // TESTRESPONSE[s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
  145. // TESTRESPONSE[s/"_seq_no" : 5/"_seq_no" : $body._seq_no/]