delete.asciidoc 6.2 KB

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