Name:
interface
Value:
Extend your Amplify Gen 2 app with AWS Blocks — self-contained backend capabilities you compose into your existing backend.

Page updated Apr 29, 2024

NewDeploy web apps with Git or your AI coding agent using the latest AWS Amplify. Add managed databases and authentication as you grow. Explore the documentation.
Maintenance ModeYou are viewing Amplify Gen 1 documentation. Amplify Gen 1 has entered maintenance mode and will reach end of life on May 1, 2027. For existing Gen 1 projects, a migration guide and tooling are available to help you upgrade.

Additional DataStore methods

These docs cover Amplify Gen 1. For new web apps, we recommend the latest AWS Amplify, with a simplified CLI and native integrations for databases, storage, and AI models. Get started with the latest AWS Amplify.

Clear

To stop DataStore sync process and to clear local data from DataStore, use the clear method:

do {
try await Amplify.DataStore.clear()
print("DataStore cleared")
} catch let error as DataStoreError {
print("Error clearing DataStore: \(error)")
} catch {
print("Unexpected error \(error)")
}

If your app uses authentication, it is recommended to call DataStore.clear() on sign-in or sign-out to remove any user-specific data. In scenarios where a mobile device can be shared by several users, calling DataStore.clear() will ensure that data does not leak from one user to another.

Start

Synchronization starts automatically whenever you run any DataStore operation (query(), save(), delete().) You can also explicitly begin the process with DataStore.start():

do {
try await Amplify.DataStore.start()
print("DataStore started")
} catch let error as DataStoreError {
print("Error starting DataStore: \(error)")
} catch {
print("Unexpected error \(error)")
}

Stop

To stop the DataStore sync process, you can use DataStore.stop(). This will close the real time subscription connection when your app is no longer interested in updates. You will typically call DataStore.stop() just before your application is closed. You can also force your DataStore sync expressions to be re-evaluated at runtime by calling stop() followed by start().

do {
try await Amplify.DataStore.stop()
print("DataStore stopped")
} catch let error as DataStoreError {
print("Error stopping DataStore: \(error)")
} catch {
print("Unexpected error \(error)")
}