Skip to content

WriteableCollection.delete()

David Fahlander edited this page Apr 10, 2014 · 29 revisions

Deletes all objects in the query.

Syntax

collection.delete()

Return Value

Promise where result is an array of arrays [key, value] of all deleted objects.

Error Handling

If any object fails to delete, entire operation will fail and transaction will be aborted.

If you catch the returned Promise, transaction will not abort, and you recieve an Error object containing the following properties:

failures Array of Error objects of all errors that have occurred
failedRecords Array of failed records. Each record is an array of [key, object]
deletedRecords Array of successfully deleted records. Each record is an array of [key, object]

If you do NOT catch the returned Promise, and an error occur, the transaction will be aborted.

If you want to log the error but still abort the transaction, you must encapsulate the operation in a transaction() block and catch the transaction instead. It is also possible to catch the operation and call transaction.abort() in the catch() clause.

Sample

db.orders.where("state").anyOf("finished", "discarded").or("date").below("2014-02-01").delete()
         .then(function (deletedOrders) {
             console.log( JSON.stringify(deletedOrders) ):
         });

Remarks

This method is equivalent to the following:

var deletedRecords = [];
collection.each(function (item, cursor) {
    deletedRecords.push([cursor.key, cursor.value]);
    cursor.delete();
}).then (function () {
    return deletedRecords;
});
Clone this wiki locally