3.6. Delete an ItemΒΆ

Items can also be deleted by primary key value. A condition expression can also be provided. If the item is deleted, awstin.dynamodb.Table.delete_item() returns True. If the condition fails, it returns False.

More information about the query/condition syntax is given in Querying and Scanning the Data.

from models import Movie

from awstin.dynamodb import DynamoDB


def delete_underrated_movie(title, year, rating):
    dynamodb = DynamoDB()
    table = dynamodb[Movie]

    return table.delete_item(
        (year, title),
        condition_expression=Movie.info.rating <= rating,
    )


if __name__ == "__main__":
    print("Attempting a conditional delete...")
    deleted = delete_underrated_movie("The Big New Movie", 2015, 5)
    if deleted:
        print("Deleted film")
    else:
        print("Did not delete film")