| by Arround The Web | No comments

DynamoDB Delete-Item Command

The DynamoDB Delete-Item operation is one of the mostly used utilities in managing the database. It helps to delete a single item from your table using the primary key. Notably, this command relies on the DynamoDB DeleteItem API. You can invoke it using the delete-item CLI command.

Performing a conditional delete operation in DynamoDB removes the existing items or items with expected attribute values. After deleting an item, you can also return the item’s attribute values using the DynamoDB ReturnValues parameter.

This article focuses on the Delete-Item operation in DynamoDB. Among the areas that you should expect to learn more about include the Delete-Item Synopsis, its options, the command line, and its example.

DynamoDB Delete-Item Operation Synopsis

The following is a complete synopsis of the Delete-Item operation:

  delete-item

--table-name <value>

--key <value>

[--expected <value>]

[--conditional-operator <value>]

[--return-values <value>]

[--return-consumed-capacity <value>]

[--return-item-collection-metrics <value>]

[--condition-expression <value>]

[--expression-attribute-names <value>]

[--expression-attribute-values <value>]

[--cli-input-json <value>]

[--generate-cli-skeleton <value>]

[--debug]

[--endpoint-url <value>]

[--no-verify-ssl]

[--no-paginate]

[--output <value>]

[--query <value>]

[--profile <value>]

[--region <value>]

[--version <value>]

[--color <value>]

[--no-sign-request]

[--ca-bundle <value>]

[--cli-read-timeout <value>]

[--cli-connect-timeout <value>]

DynamoDB Delete-Item Command Options

The following are the main options for the Delete-Item Operation:

--table-name (string)

This option specifies the table name from which you want to delete an item.

--key (map)

It is a map that designates or attaches the attribute names to the respective AttributeValue objects, representing the primary key of the item that you intend to delete. Ensure that you provide the full primary key.

For example, if the item has a simple primary key, you must provide a matching partition key. On the other hand, ensure that you give the partition key and the sort key for items with composite primary keys.

--expected (map)

The –expected option is a legacy parameter. You should use the ConditionExpression instead of the parameter.

--conditional-operator (string)

This is also a legacy parameter, and it is advisable to use the ConditionExpression parameter instead. Its possible values are AND/OR.

--return-values (string)

If you want the attributes for the item that you intend to delete as they appeared before the delete operation, ensure that you include the ReturnValues parameter. The possible values for the ReturnValues parameter when used in the Delete-Item command are NONE or ALL_OLD.

If you do not specify the return values parameter or put it as NONE, your result will not contain any attributes of the deleted item. Ideally, NONE remains the default specification for this parameter. However, an ALL_OLD specification on the ReturnValues parameter during a delete-item operation brings back the content for the old item.

--return-consumed-capacity (string)

This parameter determines the depth or level of detail about the on-demand or provisioned throughput consumption that your response should return. The possible values for this option include INDEXES, TOTAL, and NONE.

For INDEXES, your response should include the aggregate ConsumedCapacity for your operation alongside the ConsumedCapacity which is associated with each table and assessed secondary index.

On the other hand, the operations with the TOTAL specification brings back the responses that only include the aggregate ConsumedCapacity for your process. Finally, the NONE values are the default settings. They do not get back any details on the ConsumedCapacity of your operation.

--return-item-collection-metrics (string)

The –return-item-collection-metrics parameter determines whether you need the item collection response in the response. You can either set it to SIZE or NONE, with the NONE setting being the default. In a SIZE setting, your result returns the details about the item collections if any particulars exist.

--condition-expression (string)

A DeleteItem API call will not succeed if your command does not satisfy the –condition-expression requirement. This expression contains the functions, comparison parameters, and logical parameters.

How to Use the DynamoDB Delete-Item Operation and Its Example

The DynamoDB Delete-Item command exhibits two notable features. First, it is an idempotent operation which implies that if you do not specify the conditions, you can run it several times on the same item or attribute without bringing back an error response.

Second, the DynamoDB Delete-Item command is a singleton operation. This feature makes it impossible to run a single command while aiming at deleting all the rows in your table.

A simple delete-item command should look like this:

aws dynamodb delete-item \
    --table-name Music \
    --key file://key.json \
    --return-values ALL_OLD \
    --return-consumed-capacity TOTAL \
    --return-item-collection-metrics SIZE

A sample response for the previous command looks like this:

Conclusion

The DynamoDB Delete-Item command plays a significant role in updating your tables and indexes as it deletes the items from them. While this is a CLI command, you can delete the items from the DynamoDB tables using the AWS Management Console or any of AWS SDKs such as Boto3 for Python.

Share Button

Source: linuxhint.com

Leave a Reply