amplify-backend icon indicating copy to clipboard operation
amplify-backend copied to clipboard

Clarify data-loss risk when adding .secondaryIndexes() to an existing model

Open brightversion1 opened this issue 1 month ago • 2 comments

Environment information

* Amplify Gen 2 data models
* Amplify CLI
* DynamoDB via CloudFormation

Describe the bug

When adding a .secondaryIndexes((index) => [ index("someField") ]) modifier to an existing model in AWS Amplify Gen 2, I observed that all existing records in the corresponding DynamoDB table were dropped. This behavior is not currently clearly explained in the “Customize secondary indexes” documentation. ([docs.amplify.aws][1])

Reproduction steps

Steps to reproduce

  1. Create a model without any secondary index (e.g., MyModel).
  2. Seed data into the model.
  3. Update the model definition to include .secondaryIndexes((index) => [ index("myField") ]).
  4. Run npx ampx sandbox (or equivalent).
  5. Observe that the DynamoDB table is recreated and existing data is lost.

Expected behavior

Documentation should clearly state that adding, modifying, or removing a secondary index on a model will recreate the backend DynamoDB table, resulting in data loss unless handled via migration/export/import.

It should also provide:

  • A migration strategy or warning for users with existing production data.

Actual behavior

  • The doc shows how to add secondary indexes, but does not call out that doing so for a live model may delete existing data.
  • Users may incorrectly assume that adding an index is a safe incremental change.

Suggested documentation improvements

  • Add a section titled “Impact of modifying secondary indexes on existing data”.
  • Explain that changing indexes causes the CloudFormation stack to rebuild the underlying DynamoDB table.
  • Provide best practices (e.g., export data, apply schema change in a new table/environment, re-import data).
  • Possibly provide example CLI/script for export/reimport.

brightversion1 avatar Oct 22 '25 18:10 brightversion1