Prevent losing production data - Retain Policy for Amplify

Florian Gyger

Written by Florian Gyger
Florian Gyger develops web and mobile apps using React, React Native, Gatsby and AWS Amplify.

You might have experienced it yourself - hopefully in a non-production environment: when you change the name of a GraphQL type in your AWS Amplify API schema, the table with the old name is not renamed but dropped. Yes, dropped! All your data will silently be lost forever.

Of course you can now just try to never ever rename or remove a type by mistake. But as we know, mistakes can happen. Therefore, I've implemented a solution that will make sure your production data never gets lost unintentionally!


The GraphQL Retain transformer is a custom directive that you can install and use in your AWS Amplify API schema. It will then set the DeletionPolicy of the created DynamoDB tables from the default Delete to Retain.

What this does is it will make sure that those resources and its contents are not getting removed during a stack deletion. The deletion process will run through successfully, but you can still find the old DynamoDB table in your AWS admin console.

Attention: If you create a new DynamoDB table with the exact same name, it will then overwrite the retained table and its data! So creating backups are still a good thing to do from time to time 😉

Enable Retain Deletion Policy for your Amplify API DynamoDB tables

To use the graphql-retain-transformer in your project, just follow these simple steps to:

  1. Install the transformer: npm install --save graphql-retain-transformer.
  2. Edit amplify/backend/api/<YOUR_API>/transform.conf.json and append "graphql-retain-transformer" to the transformers field:
    "transformers": [
  3. In your schema.graphql file, append the @retain directive to all the @model types that you want to activate the Retain Deletion Policy for:
    type Todo @model @retain {
    id: ID!
    title: String!
    description: String

Congratulations! You can now push your environment to AWS and relax while your production data is now a lot safer!

If this feature helped you saving time and money, or just avoided some frustration, please consider starring the project on GitHub or support my open source work using GitHub Sponsors. Thank you! 🙏