HarmonyOS 清除app缓存

HarmonyOS
2024-12-23 16:06:14
1151浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit
  1. 当前没有清除缓存的接口,推荐下面:

查询缓存用storageStatistics.getCurrentBundleStats()接口。清除文件缓存,需要调用context的cacheDir获取缓存,然后调用系统文件fs接口,判断是文件或者文件夹,再分别消除缓存。 详细用法见下面的链接:

查询:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-storage-statistics-V5

function clearCache(context:common.UIAbilityContext) {
  let cacheDir = context.cacheDir
  fs.listFile(cacheDir).then((filenames) => {
    for (let i = 0;i < filenames.length; i++) {
      let dirPath =cacheDir+filenames[i] // 判断是否文件夹
      let isDirectory:boolean = false
      try {
        isDirectory = fs.statSync(dirPath).isDirectory()
      } catch (e) {
        console.log(e)
      }
      if (isDirectory) {
        fs.rmdirSync(dirPath)
      } else {
        fs.unlink(dirPath).then(() => {
          console.info('remove file succeed');
        }).catch((err:BusinessError) => {
          console.info("remove file failed with error message: " + err.message + ", error code: " + err.code);
        });
      }
    }
  })
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  1. 清除不了的数据可能包括首选项和web 首选项和Web组件里的缓存,首选项清除请参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-data-preferences-V5#clear

首选项清除操作中,请根据自己的需要使用delete或者clear,具体如下

delete:从缓存的Preferences实例中删除名为给定Key的存储键值对,可通过flush将Preferences实例持久化,使用callback异步回调

clear:清除缓存的Preferences实例中的所有数据,可通过flush将Preferences实例持久化,使用callback异步回调

web清除缓存请参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-cookie-and-data-storage-mgmt-V5

分享
微博
QQ
微信
回复
2024-12-23 18:51:55


相关问题
HarmonyOS 清除APP缓存的问题
569浏览 • 1回复 待解决
HarmonyOS APP有没有清除缓存的接口
1276浏览 • 1回复 待解决
HarmonyOS 清除Web缓存
1393浏览 • 0回复 待解决
HarmonyOS 清除缓存功能
1293浏览 • 1回复 待解决
HarmonyOS 关于获取缓存清除缓存
523浏览 • 1回复 待解决
HarmonyOS 如何清除应用缓存
571浏览 • 1回复 待解决
HarmonyOS web清除缓存问题
892浏览 • 1回复 待解决
flutter缓存如何清除
941浏览 • 1回复 待解决
HarmonyOS 如何清除Web缓存
614浏览 • 1回复 待解决
HarmonyOS web缓存清除localStorage
650浏览 • 1回复 待解决
HarmonyOS如何清除应用缓存
645浏览 • 0回复 待解决
HarmonyOS Web如何实现清除缓存
1365浏览 • 1回复 待解决
HarmonyOS 清除缓存不起作用
600浏览 • 1回复 待解决
HarmonyOS怎么清除当前应用的缓存
2542浏览 • 1回复 待解决
HarmonyOS 开发清除缓存功能实现方法
719浏览 • 1回复 待解决
HarmonyOS 是否有方法可以清除缓存
1287浏览 • 1回复 待解决
如何通过命令号清除应用缓存
2966浏览 • 1回复 待解决