uniapp app clear cache

methods: {
            // 获取本地缓存大小
            getStorageSize() {
                let that = this;
                plus.cache.calculate(function(size) {
                    let sizeCache = parseInt(size);
                    if (sizeCache == 0) {
                        that.currentSize = "0B";
                    } else if (sizeCache < 1024) {
                        that.currentSize = sizeCache + "B";
                    } else if (sizeCache < 1048576) {
                        that.currentSize = (sizeCache / 1024).toFixed(2) + "KB";
                    } else if (sizeCache < 1073741824) {
                        that.currentSize = (sizeCache / 1048576).toFixed(2) + "MB";
                    } else {
                        that.currentSize = (sizeCache / 1073741824).toFixed(2) + "GB";
                    }
                });
            },
            // 清理缓存
            clearStorage() {
                let that = this;
                let os = plus.os.name;
                if (os == 'Android') {
                    let main = plus.android.runtimeMainActivity();
                    let sdRoot = main.getCacheDir();
                    let files = plus.android.invoke(sdRoot, "listFiles");
                    let len = files.length;
                    console.log(files, len)
                    for (let i = 0; i < len; i++) {                         let filePath = '' + files[i]; // Did not find a suitable method to obtain the path, so writing can be converted into a file path                           plus.io.resolveLocalFileSystemURL(filePath, function(entry) {                             if (entry.isDirectory) {                                 entry.removeRecursively(function(entry) { //recursively delete all files and subdirectories under it                                     that.files = []                                     uni.showToast ({                                         title: 'Clear successful',                                         duration: 2000                                     });









                                    that.getStorageSize(); // recalculate cache  
                                }, function(e) {                                     console.log(e.message)                                 });                             } else {                                 entry.remove();                             }                         }, function(e) {                             console.log( 'Failed to read file path')                         });                     }                 } else { // ios                       plus.cache.clear(function() {                         uni.showToast({                             title: 'Cleared successfully',













                            duration: 2000
                        });
                        that.getStorageSize();
                    });
                }
            },
        },

clear all data 

function clearAppUserData() {

plus.android.importClass("android.app.ActivityManager");

var Context = plus.android.importClass("android.content.Context");

var am = plus.android

.runtimeMainActivity()

.getSystemService(Context.ACTIVITY_SERVICE);

am.clearApplicationUserData();

}

————————————————
Copyright statement: This article is the original article of CSDN blogger "Learn, learn and learn again", following the CC 4.0 BY-SA copyright agreement, please attach the original text for reprinting Source link and this statement.
Original link: https://blog.csdn.net/weixin_46076347/article/details/126300148

Clear File Cache - DCloud Answers

uniapp Android cache calculation, cache cleaning - DCloud Questions and Answers

Android cache clearing and computing - DCloud Q&A

Guess you like

Origin blog.csdn.net/txl910514/article/details/130322746