Deleting all files from the cache folder using "File"

Hello,

i want to delete all files inside the cache folder because i want my users to be able to easely free the memory in the settings page of my app. I googled for a while and couldn’t find a proper guide.

I’m sure there’s an easy way using the Ionic Native File plugin?

Hi @Fieel

I think everything you need is in the file plugin:

There’s a similar question w/solution on StackOverflow: Ionic 2: Get size cache directory and clear it

1 Like

Thanks! The File native plugin can indeed do the job, here’s my solution:

this.file.listDir(this.file.cacheDirectory,'org.chromium.android_webview').then((result)=>{

            console.log('files in org.chromium.android_webview directory: ', result);

            console.log('Started deleting files from cache folder!');

            for(let file of result){

                if(file.isFile == true){

                    this.file.removeFile(this.file.cacheDirectory+'/org.chromium.android_webview/', file.name).then( data => {
                        console.log('file removed: ', this.file);
                        data.fileRemoved.getMetadata(function (metadata) {
                            let name = data.fileRemoved.name;
                            let size = metadata.size ;
                            let fullPath = data.fileRemoved.fullPath;
                            console.log('Deleted file: ', name, size, fullPath) ;
                            console.log('Name: ' + name + ' / Size: ' + size) ;
                        }) ;
                    }).catch( error => {
                        file.getMetadata(function (metadata) {
                            let name = file.name ;
                            let size = metadata.size ;
                            console.log('Error deleting file from cache folder: ', error) ;
                            console.log('Name: ' + name + ' / Size: ' + size) ;
                        }) ;
                    });

                }
            }
        }) ;
2 Likes

@Fieel Thank you. It is working fine :grinning: