aws-amplify/amplify-js

Clear items from DataStore which are no longer included in selective sync expression

Opened this issue ยท 9 comments

mlxyz commented

Is your feature request related to a problem? Please describe.
When applying a more restrictive selective sync expression for DataStore, the existing content of the local storage is retained (see https://docs.amplify.aws/lib/datastore/sync/q/platform/js#reevaluate-expressions-at-runtime).
In our application, a portion of data items we sync with DataStore is newly generated every day and the sync expression only includes the items which are relevant for the current day. Without a way of clearing out "old" items which are no longer included in the sync expressions, the local storage of clients will grow indefinitely over time impacting performance and user experience.

Describe the solution you'd like
We would be fine with either of these solutions:

  • DataStore automatically clears items from local storage which are no longer included in a sync expression.
  • Allow developers to manually remove items from the DataStore (by id or using a selective expression) without deleting them from DynamoDB.

Describe alternatives you've considered
We have considered calling DataStore.clear() to remove old content when changing the sync expressions (as mentioned in the documentation), but this is inefficient as DataStore then also has to resync data items which should be retained over longer time periods.

Hey @mlxyz, we're currently looking into ways of accomplishing this. Will keep this issue updated with any developments!

@iartemiev I think Datastore.stop > Datastore.clear > Datastore.start would work as expected .

mlxyz commented

@mir1198yusuf This does work, but the problem is that DataStore resyncs everything - not just the sync expressions that have changed.

yeah correct, so what you want in this issue is to run a query locally only to discard those items.

mlxyz commented

@mir1198yusuf Yes, that's basically it. Even a way to clear individual items from DataStore only by id would suffice.

I would prefer to selectively clear a subset of data similar to https://docs.amplify.aws/lib/datastore/sync/q/platform/ios#clear-local-data
This would cover the scenario of clearing user data as recommended https://docs.amplify.aws/lib/datastore/other-methods/q/platform/ios but allow you to keep static data

In my case I wanted to ensure that after all logins (if online) data starts from zero

If you try to run the following on React Native Web at app initialisation it does not work

useEffect(() => {await DataStore.clear(); ...do something},[]}

Digging into the code the following switch checks for the initialisation of the storage that the clear method does not invoke for you
//datastore.js in node_modules
image

The solution is to explicitly call DataStore.start() before calling the clear method

Keep in mind that this also deletes all local data including potentially user data

Let me know if you have found a better solution

Any word on this? We have the same need but with amplify-flutter.

Any update on this? We would need the same functionality with flutter as well.