← Back to Home

Module 3: DynamoDB Delete

Learning Objectives

Introduction to DynamoDB Delete Operations

DynamoDB provides several ways to delete data from your tables. In this module, we focus on using the DynamoDBMapper.delete() method to remove items from DynamoDB tables. Understanding how to properly delete data is crucial for maintaining your database and ensuring data integrity throughout your application.

Delete operations in DynamoDB are designed to be idempotent, which means that you can execute the same delete operation multiple times without changing the result beyond the initial application. This is important for handling retries and error recovery in distributed systems.

Key Topics

DeleteItem Operation

Learn how to delete single items from DynamoDB tables.

  • Basic DeleteItem syntax
  • Required parameters
  • Error handling

Conditional Deletes

Add conditions to your delete operations for data validation.

  • ConditionExpression parameter
  • Expression attributes
  • Handling conditional failures

Return Values

Retrieve information about deleted items.

  • ReturnValues parameter
  • Options for returned data
  • Processing returned attributes

Batch Deletes

Delete multiple items in a single operation.

  • BatchWriteItem operation
  • Limitations and constraints
  • Handling partial failures

Deleting Items in DynamoDB

Deleting Items with a Partition Key

For DynamoDB tables that use only a partition key, you only need to specify that key value to delete an item. Let's look at an example with a Shoes table:

@DynamoDBTable(tableName = "Shoes")
public class Shoe {
    private String shoeId;
    private int cubbyLocation;
    private String color;
    private String style;
    private String occasion;

    @DynamoDBHashKey(attributeName = "shoeId")
    public String getShoeId() {
        return shoeId;
    }

    public void setShoeId(String shoeId) {
        this.shoeId = shoeId;
    }

    // Additional getters and setters...
}

To delete a shoe with shoeId = "SN01":

DynamoDBMapper mapper = new DynamoDBMapper(DynamoDbClientProvider.getDynamoDBClient());
Shoe shoe = new Shoe();
shoe.setShoeId("SN01");
mapper.delete(shoe);

Notice that we only need to set the partition key (shoeId) value. There's no need to load the complete item before deleting it.

Deleting Items with a Composite Key

For tables that use a composite key (partition key + sort key), you must specify both key values to delete an item. Here's an example with a Songs table:

@DynamoDBTable(tableName = "Songs")
public class Song {
    private String artist;
    private String songTitle;
    private String genre;
    private int year;
    private boolean favorited;

    @DynamoDBHashKey(attributeName = "artist")
    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    @DynamoDBRangeKey(attributeName = "song_title")
    public String getSongTitle() {
        return songTitle;
    }

    public void setSongTitle(String songTitle) {
        this.songTitle = songTitle;
    }

    // Additional getters and setters...
}

To delete a song by "Black Eyed Peas" with the title "I Gotta Feeling":

DynamoDBMapper mapper = new DynamoDBMapper(DynamoDbClientProvider.getDynamoDBClient());
Song song = new Song();
song.setArtist("Black Eyed Peas");
song.setSongTitle("I Gotta Feeling");
mapper.delete(song);

Idempotent Delete Operations

An important characteristic of DynamoDB delete operations is that they are idempotent. If you try to delete an item that doesn't exist, the operation completes successfully without any error or special indication. The response remains the same whether the item existed or not.

This feature is useful for handling retry scenarios where you want to ensure an item is deleted but don't need to know if it existed beforehand.

Implementing Soft Deletes

Rather than permanently deleting data, many applications implement "soft deletes" by setting a flag or status on the item. This approach preserves the data while making it appear deleted to the application. Here's how to implement a soft delete:

// Load the item first
Song song = mapper.load(Song.class, "Black Eyed Peas", "I Gotta Feeling");

// Instead of deleting, mark it as inactive
song.setActive(false);

// Save the updated item
mapper.save(song);

This approach allows you to filter out inactive items in your queries while maintaining the ability to restore or reference the data later if needed.

Best Practices for DynamoDB Deletes

Resources

AWS Documentation: DeleteItem

Official AWS documentation for the DeleteItem operation.

Code-Alongs

Additional code-along exercises for this sprint.

Sprint Challenge

Access the sprint challenge for this unit.