Search code examples
cordovaphonegap-plugins

How to delete cached files downloaded with phonegap-plugin-contentsync?


In an app we developed with cordova we need to delete files downloaded with the help of the contentsync plugin.

This is what I implemented using the file plugin.

function clearCache() {
    localStorage.clear();

    // remove the stuff from the file system as well
    deleteAllFilesInPath(cordova.file.dataDirectory);
    deleteAllFilesInPath(cordova.file.cacheDirectory);
    deleteAllFilesInPath(cordova.file.externalDataDirectory);
    deleteAllFilesInPath(cordova.file.externalCacheDirectory);
}

function deleteAllFilesInPath(path) {
    if (!path) { return; }
    window.resolveLocalFileSystemURL(path, function (entry) {
        if (entry.isDirectory) {
            var dirReader = entry.createReader();
            dirReader.readEntries(function(entries) {
                console.log(entries);
                for (var i in entries) {
                    deleteFileOrDirEntry(entries[i]);
                }
            })
        }
    })
}

function deleteLocalPath(path) {
    window.resolveLocalFileSystemURL(path,
        deleteFileOrDirEntry,
        function (error) {
            log.error("failed to access ", path, " error: ", JSON.stringify(error));
        });
}
function deleteFileOrDirEntry(entry) {
    if (entry.isDirectory) {
        entry.removeRecursively(function (code) {
                log.info("deleted dir ", entry.fullPath, " ", code);
            },
            function (error) {
                log.error("failed to remove dir ", entry.fullPath, " error: ", JSON.stringify(error))
            });
    } else {
        entry.remove(function (code) {
                log.info("deleted file ", entry.fullPath, " ", code);
            },
            function (error) {
                log.error("failed to remove file ", entry.fullPath, " error: ", JSON.stringify(error))
            });
    }
}

This works well on Android. On iOS, I got the problem that when I sync stuff again, it fails with an error:

Task ... completed with error: The operation couldn't be completed. No such file or directory
Error downloading type: 2, responseCode: 200

But when I quit the app and restart, it works.

I found no API to delete files directly in the sync plugin. Do I need to reset the plugin / inform it about removed files?


Solution

  • Ah, it seems that on iOS, the plugin gets confused if the cache dir is also cleared. If I just do:

    function clearCache() {
        localStorage.clear();
    
        // remove the stuff from the file system as well
        deleteAllFilesInPath(cordova.file.dataDirectory);
        // deleteAllFilesInPath(cordova.file.cacheDirectory);
        deleteAllFilesInPath(cordova.file.externalDataDirectory);
        // deleteAllFilesInPath(cordova.file.externalCacheDirectory);
    }
    

    it works.

    (The question remains if this way of deleting files is alright for the sync plugin or if there is / should be an API for this.)