# Update, delete local cache

**URL:** <https://community.sendbird.com/t/update-delete-local-cache/4116>\
**Category:** JavaScript\
**Tags:** chatsdk\
**Created:** [March 9, 2022, 5:10am UTC](https://community.sendbird.com/t/update-delete-local-cache/4116 "2022-03-09T05:10:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aayushthapa](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/aayushthapa/32/1910_2.png) [@aayushthapa](https://community.sendbird.com/u/aayushthapa)\
**Post date:** [March 9, 2022, 5:10am UTC](https://community.sendbird.com/t/update-delete-local-cache/4116/1 "2022-03-09T05:10:23Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/sendbird/original/2X/2/284e87064fe4eb6092fe5b9c62f6d75c41bfea61.png)  
Hi,  
On docs, it says we need to clear existing data source before adding api’s result to the local cache. How we clear and update the local cache? Also if `setLimit(50)` is set to messageCollection only 25 messages are retrieved from onApiResult callback and 15 from onCacheResult callback.

---

<div class="post-metadata">

**Author:** ![taha.saghir](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/taha.saghir/32/2094_2.png) [@taha.saghir](https://community.sendbird.com/u/taha.saghir)\
**Post date:** [March 9, 2022, 8:15pm UTC](https://community.sendbird.com/t/update-delete-local-cache/4116/3 "2022-03-09T20:15:52Z")

</div>

Hi @aayushthapa - thanks for reaching out to Sendbird!

I’m running some test for `onCacheResult` and `onApiResult`, updates to follow. In the meantime, can you please share the steps to recreate the `setLimit` scenario?

---

<div class="post-metadata">

**Author:** ![taha.saghir](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/taha.saghir/32/2094_2.png) [@taha.saghir](https://community.sendbird.com/u/taha.saghir)\
**Post date:** [March 10, 2022, 7:19pm UTC](https://community.sendbird.com/t/update-delete-local-cache/4116/4 "2022-03-10T19:19:02Z")

</div>

Here are some updates.

From my testing I’m observing that the cache is automatically getting refreshed without having to manually clear and update the cache. I have reached out to our engineering team to confirm the expected behavior.

About the `setLimit(50)` I noticed that it’s making two API calls - first with previous limit 25 and the second with the next limit 25. Since there are no messages after the current timestamp, it’s only returning the previous 25. Again, I have reached out to the engineering team to explain the expected behavior.

**UPDATE:**  
Hi @aayushthapa - only the view needs to be manually handled in `onCacheResult` and `onApiResult`. The cache is automatically updated.

Please refer to the following sample code.

> <https://github.com/sendbird/sendbird-javascript-samples/blob/0ef7e5fa32bd2ca213708ebe566b67f775bf8596/javascript/javascript-basic-local-caching/src/js/components/ChatBody.js>

---

<div class="post-metadata">

**Author:** ![aayushthapa](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/aayushthapa/32/1910_2.png) [@aayushthapa](https://community.sendbird.com/u/aayushthapa)\
**Post date:** [March 14, 2022, 12:13pm UTC](https://community.sendbird.com/t/update-delete-local-cache/4116/5 "2022-03-14T12:13:29Z")

</div>

@taha.saghir Thanks for the update. We are handling only views on those methods. Also, how can we get messages of length 50 on callback of both `onCacheResult` and `onApiResult` if `messageCollection.setLimit(50)` is used?

---

<div class="post-metadata">

**Author:** ![taha.saghir](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/taha.saghir/32/2094_2.png) [@taha.saghir](https://community.sendbird.com/u/taha.saghir)\
**Post date:** [March 14, 2022, 2:48pm UTC](https://community.sendbird.com/t/update-delete-local-cache/4116/6 "2022-03-14T14:48:56Z")

</div>

You can use the `hasPrevious` to paginate through the messages.

> **[Message collection | Chat JavaScript SDK | Sendbird Docs](https://sendbird.com/docs/chat/v3/javascript/guides/message-collection#2-pagination)**
>
> Learn how to use MessageCollection for Local caching.
