Clear cache on smartphone

Hi!
I need to programmatically clear the app cache on my smartphone, is it possible?
I am using ionic 6 Capacitor 3 and Angular 14.

Thanks!
Monica

Why?

Will be nice to know

Hi!
often with new releases the data stored in the cache of the smartphone creates problems

Indexeddb localstore?

In angular one does not generally want to clear a cach at all nor even manually

I actually wouldnt even know what cache

Not ever had issues with new versions

And which problems?

HI @svsgestser, I used Ionic Storage, Ionic Cache, semver and the App Plugin of Capacitor to archive this:

  private async clearCacheIfAppWasUpdated() {
    try {
      if (this.platform.is('capacitor')) {
        const storedVersion = await this.storageService.get('appVersion') as string;
        const { version } = await App.getInfo();
        // If it's the first start of the app, storedVersion isn't available. So set it.
        if (!storedVersion) {
          await this.storageService.set('appVersion', version);
          return;
        }
        console.log('STORED VERSION', storedVersion);
        // Clear cache, if current app version is higher than stored app version
        if (semver.compare(storedVersion, version) === -1) { // -1 means current version is larger
          await this.cache.clearAll();
          await this.storageService.set('appVersion', version); // Update app version in store
          console.log('Cache was cleared because App was updated to', version);
        }
      }
    } catch (e) {
      console.error('Error on function clearCacheIfAppWasUpdated()', e);
    }
  }
1 Like

I do something similar as well with my Vue app and Ionic Storage. I use a Vuex store while the app is running that gets synced to Ionic Storage so Vuex can be re-hydrated when the app opens fresh. My store has a hardcoded store version. Upon hydration, I check the version that is in Ionic Storage against the current version. If they are different, hydration is skipped and data gets pulled fresh from the backend API.