Search code examples
javascriptamplifyjs

How to clear amplify.store()?


I need to clear amplifyjs storage, remove all key-values. Smth similar to localStorage.clear().

Thanks in advance.


Solution

  • The docs for amplifyjs indicate that you can clear (remove) a specific storage key by storing the value null to that key:

    amplify.store( "MyKeyName", null );

    We can get all of the current storage key names with: amplify.store() and then use jQuery $.each to go through the list and clear (remove) each of the items currently stored in 'amplifyjs storage':

    $.each(amplify.store(), function (storeKey) {
        // Delete the current key from Amplify storage
        amplify.store(storeKey, null);
    });
    

    You could put this code into a function and call it or use it inline somewhere, but I'd probably add the function to amplifyjs at runtime with something like:

    amplify.clearStore = function() {
        $.each(amplify.store(), function (storeKey) {
            // Delete the current key from Amplify storage
            amplify.store(storeKey, null);
        });
    };
    

    and then call that with amplify.clearStore();