You delete a single row from the store using the
Store.delete()
method. Rows are
deleted based on a
PrimaryKey
, which you construct using
a Javascript object.
You can also require a row to match a specified version before it
will be deleted. To do this, use the
Store.deleteIfVersion()
method.
Versions are described in
Using Row Versions.
When you delete a row, you must handle the same errors as occur when you perform any write operation on the store. See Write Errors for a high-level description of these errors.
var primaryKey = {id:0}; store.delete('myTable', primaryKey, function (err) { if (err) throw err; else console.log("Row deleted."); });
You can delete multiple rows at once in a single atomic operation, so long as they all share the shard key values. Recall that shard keys are at least a subset of your primary keys. The result is that you use a partial primary key (which happens to be a shard key) to perform a multi-delete.
To delete multiple rows at once, use the
Store.multiDelete()
method.
For example, suppose you created a table like this:
CREATE TABLE myTable ( itemType STRING, itemCategory STRING, itemClass STRING, itemColor STRING, itemSize STRING, price FLOAT, inventoryCount INTEGER, PRIMARY KEY (SHARD(itemType, itemCategory, itemClass), itemColor, itemSize) )
With tables containing data like this:
Row 1:
itemType: Hats |
itemCategory: baseball |
itemClass: longbill |
itemColor: red |
itemSize: small |
price: 12.07 |
inventoryCount: 127 |
Row 2:
itemType: Hats |
itemCategory: baseball |
itemClass: longbill |
itemColor: red |
itemSize: medium |
price: 13.07 |
inventoryCount: 201 |
Row 3:
itemType: Hats |
itemCategory: baseball |
itemClass: longbill |
itemColor: red |
itemSize: large |
price: 14.07 |
inventoryCount: 39 |
Then in this case, you can delete all the rows sharing the
partial primary key Hats
,
baseball
, longbill
as follows:
var primaryKey = {itemType:'Hats', itemCategory:'baseball', itemClass:'longbill'}; store.mulitDelete('myTable', primaryKey, function (err) { if (err) throw err; else console.log("Rows deleted."); });