HarmonyOS 通过步骤2获取应用缓存目录,删除目录,通过步骤1获取缓存还是有一些字节内存,无法彻底删除干净

三方库版本:import storageStatistics from "@ohos.file.storageStatistics"; import fs from '@ohos.file.fs';

操作步骤:

let bundleStats = await storageStatistics.getCurrentBundleStats();
// 读取缓存
this.cacheSize = bundleStats.cacheSize;
const cacheDir = getContext(this).getApplicationContext().cacheDir
fs.rmdirSync(cacheDir);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
HarmonyOS
2024-12-23 15:10:28
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

cacheSize包含以下几个目录,仅清理应用沙箱路径下的cache还显示有大小是正常规格。

/data/storage/el1/base/cache

/data/storage/el1/base/haps/entry/cache

/data/storage/el2/base/cache

/data/storage/el2/base/haps/entry/cache

清理demo:

import fs from ‘@ohos.file.fs’;
let cacheDir = context.cacheDir;
@Entry
@Component
struct Clear_cache {
  clearCache() {
    // let cacheDir = getContext(this).cacheDir
    // fs.rmdirSync(cacheDir)
    // console.log(“delete !!!”)

    fs.listFile(cacheDir).then((filenames) => {
      for (let i = 0;i < filenames.length; i++) {
        // let dirPath = cacheDir+filenames[i]
        let dirPath = ${cacheDir}/${filenames[i]}
        // 判断是否文件夹
        let isDirectory
        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) => {
            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.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.

获取应用文件路径:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/application-context-stage-V5#获取应用文件路径

分享
微博
QQ
微信
回复
2024-12-23 18:39:31
相关问题
如何获取当前应用程序缓存目录
3077浏览 • 1回复 待解决
怎样获取当前app的缓存目录
1244浏览 • 2回复 待解决
HarmonyOS 缓存无法清理干净
891浏览 • 1回复 待解决
HarmonyOS 应用缓存清理不彻底
669浏览 • 1回复 待解决
用file api清理缓存目录
1399浏览 • 1回复 待解决
如何获取一些应用开发必要信息
1103浏览 • 1回复 待解决
如何删除preferences中缓存的数据?
1171浏览 • 1回复 待解决
HarmonyOS关于下载到缓存目录的问题
1667浏览 • 1回复 待解决
如何通过命令号清除应用缓存
3001浏览 • 1回复 待解决
HarmonyOS 关于获取缓存与清除缓存
562浏览 • 1回复 待解决
HarmonyOS 如何获取应用缓存并清理
624浏览 • 1回复 待解决