| {% include anchor.html edit="true" title="Delete a document" hash="delete_document"%} | |
| {% highlight js %} | |
| db.remove(doc, [options], [callback]) | |
| {% endhighlight %} | |
| Or: | |
| {% highlight js %} | |
| db.remove(docId, docRev, [options], [callback]) | |
| {% endhighlight %} | |
| Deletes the document. `doc` is required to be a document with at least an `_id` and a `_rev` property. Sending the full document will work as well. | |
| See [filtered replication](http://pouchdb.com/api.html#filtered-replication) for why you might want to use `put()` with `{_deleted: true}` instead. | |
| #### Example Usage: | |
| {% include code/start.html id="delete_doc" type="callback" %} | |
| {% highlight js %} | |
| db.get('mydoc', function(err, doc) { | |
| if (err) { return console.log(err); } | |
| db.remove(doc, function(err, response) { | |
| if (err) { return console.log(err); } | |
| // handle response | |
| }); | |
| }); | |
| {% endhighlight %} | |
| {% include code/end.html %} | |
| {% include code/start.html id="delete_doc" type="async" %} | |
| {% highlight js %} | |
| try { | |
| var doc = await db.get('mydoc'); | |
| var response = await db.remove(doc); | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| {% endhighlight %} | |
| {% include code/end.html %} | |
| {% include code/start.html id="delete_doc" type="promise" %} | |
| {% highlight js %} | |
| db.get('mydoc').then(function(doc) { | |
| return db.remove(doc); | |
| }).then(function (result) { | |
| // handle result | |
| }).catch(function (err) { | |
| console.log(err); | |
| }); | |
| {% endhighlight %} | |
| {% include code/end.html %} | |
| #### Example Response: | |
| {% highlight js %} | |
| { | |
| "ok": true, | |
| "id": "mydoc", | |
| "rev": "2-9AF304BE281790604D1D8A4B0F4C9ADB" | |
| } | |
| {% endhighlight %} | |
| You can also delete a document by just providing an `id` and `rev`: | |
| {% include code/start.html id="delete_doc3" type="callback" %} | |
| {% highlight js %} | |
| db.get('mydoc', function(err, doc) { | |
| if (err) { return console.log(err); } | |
| db.remove(doc._id, doc._rev, function(err, response) { | |
| if (err) { return console.log(err); } | |
| // handle response | |
| }); | |
| }); | |
| {% endhighlight %} | |
| {% include code/end.html %} | |
| {% include code/start.html id="delete_doc3" type="async" %} | |
| {% highlight js %} | |
| try { | |
| var doc = await db.get('mydoc'); | |
| var response = await db.remove(doc._id, doc._rev); | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| {% endhighlight %} | |
| {% include code/end.html %} | |
| {% include code/start.html id="delete_doc3" type="promise" %} | |
| {% highlight js %} | |
| db.get('mydoc').then(function(doc) { | |
| return db.remove(doc._id, doc._rev); | |
| }).then(function (result) { | |
| // handle result | |
| }).catch(function (err) { | |
| console.log(err); | |
| }); | |
| {% endhighlight %} | |
| {% include code/end.html %} | |
| You can also delete a document by using `put()` with `{_deleted: true}`: | |
| {% include code/start.html id="delete_doc2" type="callback" %} | |
| {% highlight js %} | |
| db.get('mydoc', function(err, doc) { | |
| if (err) { return console.log(err); } | |
| doc._deleted = true; | |
| db.put(doc, function(err, response) { | |
| if (err) { return console.log(err); } | |
| // handle response | |
| }); | |
| }); | |
| {% endhighlight %} | |
| {% include code/end.html %} | |
| {% include code/start.html id="delete_doc2" type="async" %} | |
| {% highlight js %} | |
| try { | |
| var doc = await db.get('mydoc'); | |
| doc._deleted = true; | |
| var result = await db.put(doc); | |
| } catch (err) { | |
| console.log(err); | |
| } | |
| {% endhighlight %} | |
| {% include code/end.html %} | |
| {% include code/start.html id="delete_doc2" type="promise" %} | |
| {% highlight js %} | |
| db.get('mydoc').then(function(doc) { | |
| doc._deleted = true; | |
| return db.put(doc); | |
| }).then(function (result) { | |
| // handle result | |
| }).catch(function (err) { | |
| console.log(err); | |
| }); | |
| {% endhighlight %} | |
| {% include code/end.html %} |