# Programmatically clear cache for specific view

**URL:** <https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222>\
**Category:** ionic-v1\
**Created:** [January 7, 2015, 6:11am UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222 "2015-01-07T06:11:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![KyleT](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/kylet/32/74682_2.png) [@KyleT](https://forum.ionicframework.com/u/KyleT)\
**Post date:** [January 7, 2015, 6:11am UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222/1 "2015-01-07T06:11:12Z")

</div>

Is there a way to programmatically clear the cache for a view?

For example I have a view that upon loading fetches the person from a server:

person/:personId

On another view I perform updates to that person, persist them to server (and perform some server processing on updates) and navigate back to ‘person/:personId’. Since the view ‘person/:personId’ is cached the person is not refetched. Ideally I could programmatically do something similar to:

persistPersonToServer();  
clearViewCacheFor(‘person/:personId’);  
navigateTo(‘person/:personId’);

Thanks!

---

<div class="post-metadata">

**Author:** ![sreekanth](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sreekanth/32/67832_2.png) [@sreekanth](https://forum.ionicframework.com/u/sreekanth)\
**Post date:** [January 7, 2015, 7:49am UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222/2 "2015-01-07T07:49:11Z")

</div>

@KyleT You could listen on **$ionicView.beforeEnter** event in your view controller and fetch the data from server, before view becomes active. Something like this -

```
    .controller('ViewCtrl', function($scope) {
        $scope.$on('$ionicView.beforeEnter', function(){
            if ($scope.personUpdated) {
                $scope.person = fetchPerson(personId);
                $scope.personUpdated = false;
            }
        });
    });
```

---

<div class="post-metadata">

**Author:** ![GaryTyre](https://avatars.discourse-cdn.com/v4/letter/g/dec6dc/32.png) [@GaryTyre](https://forum.ionicframework.com/u/GaryTyre)\
**Post date:** [January 7, 2015, 2:20pm UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222/3 "2015-01-07T14:20:49Z")

</div>

I’ve been trying to solve the exact problem, let me know if you find a way to do so.

Thanks

---

<div class="post-metadata">

**Author:** ![KyleT](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/kylet/32/74682_2.png) [@KyleT](https://forum.ionicframework.com/u/KyleT)\
**Post date:** [January 7, 2015, 3:52pm UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222/4 "2015-01-07T15:52:49Z")

</div>

Thanks @sreekanth!

@GaryTyre I used $scope.$on(’$ionicView.beforeEnter’ in combination with sessionStorage to solve my issue.

Similar to (pseudo-code):

```
.controller('PersonDetailCtrl', function($scope) {
    $scope.person = {};
    $scope.$on('$ionicView.beforeEnter', function(){
        if (window.sessionStorage.getItem('refresh-personId') && $scope.loaded) {
            $scope.person = $scope.fetchPerson(personId);
            window.sessionStorage.removeItem('refresh-personId') ;
        }
    });
    $scope.fetchPerson(personId);
});
```

---

<div class="post-metadata">

**Author:** ![jtltgl](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jtltgl/32/72696_2.png) [@jtltgl](https://forum.ionicframework.com/u/jtltgl)\
**Post date:** [January 8, 2015, 4:12pm UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222/5 "2015-01-08T16:12:40Z")

</div>

Sorry, I think this is topic I need. My problem is I am getting a scrambled view when switch between running an App and the Intro instructions. I think it’s a caching problem. Here is a picture of what I am getting.

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

This is just a simple password generator and the problem happens when I switch from App to Intro. Does this sound like a cache problem? I notice when switching back to Intro the background is blue, which is background color of App. The Intro background should be white so somehow I am confusing the operation between App and Intro. If I need to post code I have no problem because all I am trying to do is release this as a free App. One more thing I notice is that the problem does not happen consistently but it is an issue. The ionic version I am using is 1.3.0 — thank you

---

<div class="post-metadata">

**Author:** ![elebetsamer](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/elebetsamer/32/66559_2.png) [@elebetsamer](https://forum.ionicframework.com/u/elebetsamer)\
**Post date:** [February 17, 2015, 9:26pm UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222/6 "2015-02-17T21:26:33Z")

</div>

I would also like to see the ability to clear cache for a certain view added in to Ionic core. I just had this need pop up last night. For now, I’m just using `clearCache()` and clearing the full cache. It seems a bit less clunky than having to rely on `beforeEnter`. Though I am also using `beforeEnter` in some places to make sure some of the data in my view is fresh.

---

<div class="post-metadata">

**Author:** ![soocheng](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@soocheng](https://forum.ionicframework.com/u/soocheng)\
**Post date:** [July 21, 2015, 1:13pm UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222/7 "2015-07-21T13:13:04Z")

</div>

can clear a specific view by using

```
$ionicHistory.clearCache([stateId]);

```

note: you can’t clear a page’s cache that you are currently on.

---

<div class="post-metadata">

**Author:** ![jackyon](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jackyon/32/69735_2.png) [@jackyon](https://forum.ionicframework.com/u/jackyon)\
**Post date:** [August 13, 2015, 6:35am UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222/8 "2015-08-13T06:35:00Z")

</div>

thx! your solution worked for me!

---

<div class="post-metadata">

**Author:** ![herve76](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@herve76](https://forum.ionicframework.com/u/herve76)\
**Post date:** [September 30, 2015, 5:46pm UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222/9 "2015-09-30T17:46:53Z")

</div>

@soocheng $ionicHistory.clearCache([stateId]); does not work for me. Any reason for this? I am trying to clean the cache of another view I am not in.

---

<div class="post-metadata">

**Author:** ![mattstueve](https://avatars.discourse-cdn.com/v4/letter/m/bc8723/32.png) [@mattstueve](https://forum.ionicframework.com/u/mattstueve)\
**Post date:** [October 1, 2015, 11:06pm UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222/10 "2015-10-01T23:06:23Z")

</div>

You can also disable cache for a specific state in the ui-router by setting the cache property to false:

```
            .state("profileEditAddress", {
                url : "/profileEditAddress/:id",
                controller : "ProfileEditAddressCtrl",
                cache : false,
                templateUrl : "components/profileEditAddress/profileEditAddress.html"
            })
```

---

<div class="post-metadata">

**Author:** ![soocheng](https://avatars.discourse-cdn.com/v4/letter/s/91b2a8/32.png) [@soocheng](https://forum.ionicframework.com/u/soocheng)\
**Post date:** [October 22, 2015, 5:50am UTC](https://forum.ionicframework.com/t/programmatically-clear-cache-for-specific-view/15222/11 "2015-10-22T05:50:29Z")

</div>

you can try to log the stateId of your another view,  
the stateId of your view might be appended with its state params  
eg:  
your state name is ‘article’ with state params ‘id’,  
your stateId might be article\_id\_blabla
