Web Niraj
  • Facebook
  • Flickr
  • Github
  • Instagram
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
  • Apps
    • Gemini Quota App for macOS
Search the site...
  • Home
  • Blog
  • Cordova: Deleting a File using the File Plugin

Cordova: Deleting a File using the File Plugin

0

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

// access the persistent file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
// get the file named "config.json"
fs.root.getFile("config.json", { create: false }, function(fileEntry) {
// attempt to remove the file if it exists
fileEntry.remove(function() {
// delete successful
console.info('Config file has been deleted successfully.');
}, function(error) {
// delete failed
console.error('Could not delete Config file. ' + JSON.stringify( error ));
});
});
});
view raw delete_file.js hosted with ❤ by GitHub

The Explanation

  1. You first need to request the file system to access (line 2). This can be either LocalFileSystem.PERSISTENT for the persistent file system, or window.TEMPORARY for temporary storage (which will get deleted as space is needed).
  2. 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 set create to false, so the file isn’t created if it doesn’t exist. The getFile function also accepts a forth parameter for the errorCallback, which is called if the file cannot be found.
  3. Finally, we delete the file if it exists (line 8). Here we have both the successCallback and errorCallback specified.
Cordova, JavaScript, Tutorial

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Subscribe for updates

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Flat File Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux Mac OS MongoDB NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins

© 2011-2026 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy