# clearCache() possible bug?

**URL:** <https://forum.ionicframework.com/t/clearcache-possible-bug/16532>\
**Category:** ionic-v1\
**Created:** [January 28, 2015, 3:14pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532 "2015-01-28T15:14:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JerryBels](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jerrybels/32/7502_2.png) [@JerryBels](https://forum.ionicframework.com/u/JerryBels)\
**Post date:** [January 28, 2015, 3:14pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/1 "2015-01-28T15:14:08Z")

</div>

Hello guys,

So I tried to use clearCache() in my app to clear all cached views whenever reaching a specific state. I tried with a function when the button leading to that state is clicked :

```
$scope.switchEvent = function() {
    $ionicHistory.clearCache();

    $state.go("welcome");
}

```

That call was from a nested state (called “app.main”). Didn’t work. When clicking on the link that gets me back to “app.main” with a different parameter for the page (should be different data), the cached view shows up, with the old data. Note that if using the inline

```
cache-view="false"

```

Directly on the view, it works.

So I tried something else. On the “.run” function, I added :

```
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
    if(toState.name == "welcome")
        $ionicHistory.clearCache();
});

```

Using this didn’t work either. The clearCache() was triggered (I checked it with a console.log) but didn’t clear any cached view. But if I used no condition and launch clearCache() on every view change, it worked. I ended up with this

```
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
    if(fromState.name.substr(0, 4) != "app." && (toState.name == "app.main" || toState.name == "app.mainSecondary"))
        $ionicHistory.clearCache();
});

```

So this works, but I’m not satisfied as it’s not very robust… Apparently only clearCache() when changing to the wanted view works. Not sure if it’s linked to the nested states or what, but there you go…

@adam I wanted to reference this issue in [Please help test: Angular 1.3, improved transitions, cached views, etc](http://forum.ionicframework.com/t/please-help-test-angular-1-3-improved-transitions-cached-views-etc/12528) but it’s closed ☀

---

<div class="post-metadata">

**Author:** ![seanhill](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/seanhill/32/105765_2.png) [@seanhill](https://forum.ionicframework.com/u/seanhill)\
**Post date:** [January 30, 2015, 4:18pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/2 "2015-01-30T16:18:04Z")

</div>

Hey @JerryBels I’m having a very similar issue. I have it in my `logout` function.

```
$scope.logout = function() {

	$ionicHistory.clearCache();
	$ionicHistory.clearHistory();
	$state.go('home');

};

```

However, after inspecting the DOM after my logout, there is still plenty of cached views, and I can still navigate backwards through my history, aka, it looks like those two functions did nothing at all.

![image](https://global.discourse-cdn.com/ionicframework/original/2X/a/aa6415d319076e8309bad3f4075c6f93d97d8e13.png)

@mhartington any help here?

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [January 30, 2015, 4:48pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/3 "2015-01-30T16:48:37Z")

</div>

Hmm it does seem to work, but its a bit tricky.

> **[Tabs And Navigation: Nightly](https://codepen.io/mhartington/pen/ZYJPvR)**
>
> Demo showing how each tab has its own history stack. Navigating between each tab remembers the correct back and forward views that were visited. Forke...

---

<div class="post-metadata">

**Author:** ![seanhill](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/seanhill/32/105765_2.png) [@seanhill](https://forum.ionicframework.com/u/seanhill)\
**Post date:** [January 30, 2015, 7:10pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/4 "2015-01-30T19:10:30Z")

</div>

@mhartington my situation is a little different. Where the `logout` function is on my most parent ctrl, for instance a ctrl connected to the `body` tag. I want to be able to call `logout` anywhere in the app and be able to clear my history and cache, and just drop them back on my `home` screen. In your example you use the `afterLeave` event on a child controller to remove the history/cache. However, for me, maybe I don’t want to clear out the history/cache everytime a user navigates away from that view, but only when they use the `logout` functionality.

It seems like I’d have to duplicate a lot of code to be able to logout in different views if the clear history / cache isn’t in the most parent `logout` function.

---

<div class="post-metadata">

**Author:** ![paulslugocki](https://avatars.discourse-cdn.com/v4/letter/p/cab0a1/32.png) [@paulslugocki](https://forum.ionicframework.com/u/paulslugocki)\
**Post date:** [January 30, 2015, 7:58pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/5 "2015-01-30T19:58:28Z")

</div>

That’s pretty much what I do / want to do too.

---

<div class="post-metadata">

**Author:** ![JerryBels](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jerrybels/32/7502_2.png) [@JerryBels](https://forum.ionicframework.com/u/JerryBels)\
**Post date:** [January 31, 2015, 4:39pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/6 "2015-01-31T16:39:47Z")

</div>

I will try it tomorrow and report how it went for me… But anyway it _should_ work right away as in my example, no ?

Something else : the caching doesn’t take into account the $stateParams, which means the same page is displayed for user/24 and user/77 and that is bad. IMHO when navigating to a view that has stateParams, it should check them and if they’re not the same as in the cache, clear the cache for that view and display the new one (to avoid having a lot of the same view cached, only the last one should be kept).

---

<div class="post-metadata">

**Author:** ![JerryBels](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jerrybels/32/7502_2.png) [@JerryBels](https://forum.ionicframework.com/u/JerryBels)\
**Post date:** [February 2, 2015, 1:21pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/7 "2015-02-02T13:21:47Z")

</div>

Okay, after testing… @seanhill +1

---

<div class="post-metadata">

**Author:** ![cquartier](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cquartier/32/76449_2.png) [@cquartier](https://forum.ionicframework.com/u/cquartier)\
**Post date:** [February 9, 2015, 9:39pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/8 "2015-02-09T21:39:42Z")

</div>

@seanhill I have the same problem. For now I’m disabling the cache throughout my whole app because if I can’t reliably clear the cache on logout then I have data security concerns. I think the problem might be how the `$ionNavViewDelegate` instances are tracked in the framework. The `$ionicHistory.clearCache` function is looping through those instances and calling the clearCache function on each of them (which presumably should destroy the DOM elements and scopes for every view in your app). It seems like they might expect an instance per cached view but I’m only ever seeing 2 instances being cleared, the one where my logout function exists, and its parent.

It would be nice if we could get an Ionic/Driftyco developer to weigh in on this, if nothing else than to acknowledge that the cache-clearing mechanism doesn’t appear to be working the way most of us expect.

---

<div class="post-metadata">

**Author:** ![JerryBels](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jerrybels/32/7502_2.png) [@JerryBels](https://forum.ionicframework.com/u/JerryBels)\
**Post date:** [February 15, 2015, 5:55pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/9 "2015-02-15T17:55:56Z")

</div>

For those interested I tried to help the devs here : [https://github.com/driftyco/ionic/issues/2939#issuecomment-74427724](https://github.com/driftyco/ionic/issues/2939#issuecomment-74427724)

Since I don’t consider myself already really good with Angular, everyone is welcome to improve the demo 😄

---

<div class="post-metadata">

**Author:** ![bostondv](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bostondv/32/64610_2.png) [@bostondv](https://forum.ionicframework.com/u/bostondv)\
**Post date:** [February 19, 2015, 9:18pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/10 "2015-02-19T21:18:21Z")

</div>

@seanhill @cquartier I’m also seeing this issue on beta 14 and beta.14-nightly-1060.

My app is based off ionic-starter-tabs. I have 4 tab states and a separate state for login. I’m calling `$ionicHistory.clearCache()` and `$ionicHistory.clearHistory()` from a controller within one of the tabs. For me, it removes all the cached tabs from the DOM _except_ for the tab which logout is called from.

So when logging back in, that tab remains cached and displays old user data.

---

<div class="post-metadata">

**Author:** ![pjusto](https://avatars.discourse-cdn.com/v4/letter/p/db5fbb/32.png) [@pjusto](https://forum.ionicframework.com/u/pjusto)\
**Post date:** [March 10, 2015, 2:24pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/11 "2015-03-10T14:24:34Z")

</div>

@mhartington

When you have cached views enabled, the controller does not run again. even if you clear cache with `$ionicHistory.clearCache()`;

I think it is clearly a bug.  
I hope it helps 😉

---

<div class="post-metadata">

**Author:** ![bostondv](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bostondv/32/64610_2.png) [@bostondv](https://forum.ionicframework.com/u/bostondv)\
**Post date:** [March 10, 2015, 2:46pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/12 "2015-03-10T14:46:20Z")

</div>

I was able to work around my problem case by calling `$ionicHistory.clearCache()` and `$ionicHistory.clearHistory()` on logout AND login.

When the user logs out from the settings page and gets taken to the login page, the settings page is still cached. When another user logs in I clear cache again which successfully removes the cached settings view.

---

<div class="post-metadata">

**Author:** ![devendraverma](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/devendraverma/32/77316_2.png) [@devendraverma](https://forum.ionicframework.com/u/devendraverma)\
**Post date:** [April 13, 2015, 10:32am UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/13 "2015-04-13T10:32:17Z")

</div>

@bostondv thanks for suggestion

---

<div class="post-metadata">

**Author:** ![TheMafia](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@TheMafia](https://forum.ionicframework.com/u/TheMafia)\
**Post date:** [May 28, 2015, 6:34am UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/14 "2015-05-28T06:34:04Z")

</div>

Hi everyone. Is there any solution for this problem? I also cant clear cache by `$ionicHistory.clearCache();`. I tried do this on user logout, then on logout AND login. Nothing changes, the page on which the user appears after login still show cached data. Any suggestions?

---

<div class="post-metadata">

**Author:** ![JerryBels](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jerrybels/32/7502_2.png) [@JerryBels](https://forum.ionicframework.com/u/JerryBels)\
**Post date:** [May 28, 2015, 7:42am UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/15 "2015-05-28T07:42:44Z")

</div>

Hi! I don’t think it will help you, but I deactivated completely the cache. As long as there are so many people that don’t understand how it’s said to work, and it’s not doing things logically, as far as I’m concerned it’s not production ready.

Good luck!

---

<div class="post-metadata">

**Author:** ![leoliaolei](https://avatars.discourse-cdn.com/v4/letter/l/58f4c7/32.png) [@leoliaolei](https://forum.ionicframework.com/u/leoliaolei)\
**Post date:** [May 30, 2015, 12:21pm UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/16 "2015-05-30T12:21:03Z")

</div>

> [@bostondv](#):
>
> $ionicHistory.clearCache()

It seems `$ionicHistory.clearCache()` does not work alone. I have to combine `$ionicHistory.clearCache()` and `$ionicHistory.clearHistory()` to make it work.

---

<div class="post-metadata">

**Author:** ![ho\_oh](https://avatars.discourse-cdn.com/v4/letter/h/c4cdca/32.png) [@ho\_oh](https://forum.ionicframework.com/u/ho_oh)\
**Post date:** [July 1, 2015, 10:42am UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/17 "2015-07-01T10:42:31Z")

</div>

Have this issue been fixed? Because i’m using ionic 1.0.0 and when i use clearCache() i think it clears other views except the current view. Could you suggest me any trick to deal with it?

---

<div class="post-metadata">

**Author:** ![TheMafia](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@TheMafia](https://forum.ionicframework.com/u/TheMafia)\
**Post date:** [July 2, 2015, 9:36am UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/18 "2015-07-02T09:36:07Z")

</div>

Check [this issue.](http://forum.ionicframework.com/t/ionic-cache-and-history-clean-issue/25985)

---

<div class="post-metadata">

**Author:** ![phan\_nguyen\_chuong](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@phan\_nguyen\_chuong](https://forum.ionicframework.com/u/phan_nguyen_chuong)\
**Post date:** [July 9, 2015, 4:01am UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/19 "2015-07-09T04:01:03Z")

</div>

We can try this:

```
$scope.logout = function() {
     $state.go('...');
    // Clear all cache and history
    $timeout(function () {
        $ionicHistory.clearCache();
        $ionicHistory.clearHistory();
    }, 1500)
  }

```

Note: Remember include $timeout and $state in controller options

---

<div class="post-metadata">

**Author:** ![Ramesh](https://avatars.discourse-cdn.com/v4/letter/r/d07c76/32.png) [@Ramesh](https://forum.ionicframework.com/u/Ramesh)\
**Post date:** [October 29, 2015, 7:43am UTC](https://forum.ionicframework.com/t/clearcache-possible-bug/16532/20 "2015-10-29T07:43:45Z")

</div>

Hi Friends, I hope this will works for after user logout the back button will be disabled.

$ionicHistory.nextViewOptions({  
disableAnimate: true,  
disableBack: true  
});

[Next page](https://forum.ionicframework.com/t/clearcache-possible-bug/16532.md?page=2)
