3

I'm trying to delete a directory and it's contents with PhoneGap on Android using:

deleteDirectory = function deleteDirectory(uri) {
    uri = uri.substring(0, uri.lastIndexOf('/'));
    return $.Deferred(function (def) {
        fileSystem.root.getDirectory(uri, {
            create: false
        }, function (directory) {
            directory.removeRecursively();
            def.resolve();
        }, function (error) {
            resolveError("Error deleting directory: ", error, def);
        });
    }).promise();
}

with the following error: File No Modification Allowed Error

I've confirmed this permission is set:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Where else should I be looking?

Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
user1247057
  • 109
  • 2
  • 11
  • What directory are you trying to delete? Is it on the sd card or internal storage? – Simon MacDonald Nov 13 '12 at 19:55
  • A sub folder added in the Nexus 7's Download folder: `file:///storage/sdcard0/Download/somedir/dirtodelete` I guess this would be considered internal so the `WRITE_EXTERNAL_STORAGE` will not work... will add `WRITE_INTERNAL_STORAGE` to see if that's it. – user1247057 Nov 13 '12 at 23:46
  • WRITE_INTERNAL_STORAGE didn't seem to work either. – user1247057 Nov 14 '12 at 00:00
  • You are not the first person to complain about odd behaviour on the Nexus 7. I have to see if someone has one to run the test suite. – Simon MacDonald Nov 14 '12 at 02:04

2 Answers2

10

I have done it with this approach:



    function ClearDirectory() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
        function fail(evt) {
            alert("FILE SYSTEM FAILURE" + evt.target.error.code);
        }
        function onFileSystemSuccess(fileSystem) {
            fileSystem.root.getDirectory(
                 "yours/dir/ect/ory",
                {create : true, exclusive : false},
                function(entry) {
                entry.removeRecursively(function() {
                    console.log("Remove Recursively Succeeded");
                }, fail);
            }, fail);
        }
    }


marcin
  • 99
  • 1
  • 4
3

From this answer:

I'd suggest using resolveLocalFileSystemURL if you want to access locations under cordova.file.* (eg cordova.file.dataDirectory), which is most of the time (if not always), and use requestFileSystem if you need to have access to the root of the filesystem.

This also saves some lines of code and is more readable:

deleteFolder(fileName: string) {
    const uri = `${cordova.file.dataDirectory}${fileName}`;
    window.resolveLocalFileSystemURL(uri, (dirEntry: DirectoryEntry) => {
        dirEntry.removeRecursively(
            () => console.log('successfully deleted the folder and its content'),
            e => console.error('there was an error deleting the directory', e.toString())
        )
    });
}

And here an awaitable version:

deleteFolder(fileName: string): Promise<void> {
    const promise = new Promise<void>((resolve, reject) => {
        const uri = `${cordova.file.dataDirectory}${fileName}`;
        window.resolveLocalFileSystemURL(uri, (dirEntry: DirectoryEntry) => {
            dirEntry.removeRecursively(() => resolve(), e => reject(e));
        }, e => reject(e));
    });
    return promise;
}
Yannic Hamann
  • 4,655
  • 32
  • 50