You delete a single row from the store using the
Store.delete()
method. Rows are
deleted based on a
dictionary that defines the full primary key for the row that
you want to delete.
You can also require a row to match a specified version before it
will be deleted. To do this, use the
Store.delete_if_version()
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 Exceptions for a high-level description of these errors.
try: # To delete a table row, just include a dictionary # that contains all the fields needed to create # the primary key. primary_key_d = {"item" : "bolts"} ret = store.delete("myTable", primary_key_d) if ret[0]: logging.debug("Row deletion succeeded") else: logging.debug("Row deletion failed.") except IllegalArgumentException, iae: logging.error("Row deletion failed.") logging.error(iae.message)
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.multi_delete()
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:
try: primary_key_d = {'itemType' : 'Hats', 'itemCategory' : 'baseball', 'itemClass' : 'longbill'} ret = store.multi_delete("myTable", primary_key_d) if ret > 0: logging.debug("%s rows deleted" % ret) else: logging.debug("No rows deleted.") except IllegalArgumentException, iae: logging.error("Row deletion failed.") logging.error(iae.message)