Page updated Jan 16, 2024

Additional DataStore methods

Clear

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

1Future<void> clearStore() async {
2 try {
3 await Amplify.DataStore.clear();
4 print('DataStore is cleared.');
5 } on DataStoreException catch (e) {
6 print('Failed to clear DataStore: $e');
7 }
8}

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(), observe().) You can also explicitly begin the process with DataStore.start():

1Future<void> startStore() async {
2 try {
3 await Amplify.DataStore.start();
4 print('DataStore is started.');
5 } on DataStoreException catch (e) {
6 print('Failed to start DataStore: $e');
7 }
8}

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().

1Future<void> stopStore() async {
2 try {
3 await Amplify.DataStore.stop();
4 print('DataStore is stopped.');
5 } on DataStoreException catch (e) {
6 print('Failed to stop DataStore: $e');
7 }
8}