2

I use cordova-plugin-file for ionic 2.

I know the path of the cache directory, here is my code:

this.file.resolveDirectoryUrl(this.file.cacheDirectory).then(data => {
    console.log(JSON.stringify(data)) ;
});

Is it possible in Ionic to know a directory's size and clear all files in it?

Anth0
  • 3,004
  • 6
  • 26
  • 36
wstudiokiwi
  • 880
  • 1
  • 15
  • 33

2 Answers2

3

My solution:

this.file.listDir(this.file.cacheDirectory,'').then((result)=>{
    for(let file of result){
        if(file.isFile == true){

            if(deleteFiles == 1) this.file.removeFile(this.file.cacheDirectory, file.name) ;
            else {
                file.getMetadata(function (metadata) {
                    let name = file.name ;
                    let size = metadata.size ;
                    console.log('Name: ' + name + ' / Size: ' + size) ;
                    sizes += size ;
                    }) ;
                }
        }
    }
}) ;
wstudiokiwi
  • 880
  • 1
  • 15
  • 33
1

Yes, you can.You have to use below methods.

getFreeDiskSpace() - Get free disk space in Bytes

Returns: Promise Returns a promise that resolves with the remaining free disk space in Bytes

removeRecursively(path, dirName) - Removes all files and the directory from a desired location.

path string Base FileSystem. Please refer to the iOS and Android filesystems above

dirName string Name of directory

Returns: Promise Returns a Promise that resolves with a RemoveResult or rejects with an error.

Official doc is here.

Sampath
  • 63,341
  • 64
  • 307
  • 441