The Apache Cordova documentation is rather good, but occasionally you encounter information that is either out-of-date or missing entirely. One such instance is the ability to delete a file when using the File plugin – the documentation for this function is missing, so to help others, I’ve included some sample code to achieve this.
Unless you dig into the plugin code, you wouldn’t know that the function to delete a file is called remove
, which accepts a successCallback
and errorCallback
. The code to delete a file is quite simple.
The Code
The Explanation
- You first need to request the file system to access (line 2). This can be either
LocalFileSystem.PERSISTENT
for the persistent file system, orwindow.TEMPORARY
for temporary storage (which will get deleted as space is needed). - Next, we need to get the file we want to delete (line 5). In the above example, we’re trying to access the file called
config.json
. Note that we setcreate
tofalse
, so the file isn’t created if it doesn’t exist. ThegetFile
function also accepts a forth parameter for theerrorCallback
, which is called if the file cannot be found. - Finally, we delete the file if it exists (line 8). Here we have both the
successCallback
anderrorCallback
specified.