Home > front end >  Deleting and Updating table in DynamoDB in a single Transaction
Deleting and Updating table in DynamoDB in a single Transaction

Time:10-15

Is it possible to delete all rows from a table and then update the table in a single transaction? I found this documentation: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/transaction-example.html I tried implementing it for a single item:

productItemKey.put("test_id", new AttributeValue(String.valueOf(150)));

            machine_ids.add(0, String.valueOf(newAssignments.get(150).get(0).getID()));
            machine_ids.add(1, String.valueOf(newAssignments.get(150).get(1).getID()));
            machine_ids.add(2, String.valueOf(newAssignments.get(150).get(2).getID()));
                

            Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
            expressionAttributeValues.clear();
            expressionAttributeValues.put("test_id", new AttributeValue(String.valueOf(150)));
            expressionAttributeValues.put("assignments", new AttributeValue().withSS(machine_ids));
            expressionAttributeValues.put("needsMoreMachines", new AttributeValue().withBOOL(output.doesCTneedMoreMachines(150)));

            Delete deleteItems = new Delete()
                .withTableName("test_table")
                .withKey(productItemKey);


            Put markItemSold = new Put()
                .withTableName("test_table")
                .withItem(expressionAttributeValues);

            Collection<TransactWriteItem> actions = Arrays.asList(
                new TransactWriteItem().withDelete(deleteItems),
                new TransactWriteItem().withPut(markItemSold));


            TransactWriteItemsRequest placeOrderTransaction = new TransactWriteItemsRequest()
                .withTransactItems(actions);

            try {
                client.transactWriteItems(placeOrderTransaction);
                System.out.println("Transaction Successful");
            ...

But i keep getting this error:

Transaction request cannot include multiple operations on one item 

CodePudding user response:

You can manipulate up to 100 (separate) items in a transaction. You could only delete all rows if you have fewer than 100 rows.

The best way to bulk delete all rows is to delete the table. You cannot do that transactionally with the creation of the table again. Better to make a new table with a new name and then delete the old.

If you explained what your fundamental goals are we could give some advice.

  • Related