Get Available Free Disk Space Using Apache Cordova

It was brought to my attention that the File plugin for Apache Cordova has no documented methods for finding the available disk space on a device.

This does not mean that you cannot figure out how much space is available on your users device because you can make use of Apache Cordova’s nifty cordova.exec function.

In your Apache Cordova, Phonegap, or Ionic Framework project, make sure that you have the Apache Cordova File plugin installed.  If you’re not sure how to do this, using a command prompt or Terminal, navigate to your project’s root and run the following:

You should already have the Android or iOS platform added before trying to install the plugin as it could result in strange behavior if you try to add a platform after.

Now let’s take a look at the following:

The above code will call the getFreeDiskSpace method of the File plugin and depending on the success or error callback, give you a result.  If the success callback is reached, you’ll get the total free disk space in kilobytes.  You can easily convert to megabytes or gigabytes with simple math.

A video version of this article can be seen below.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

  • To build upon this tutorial, I’ve also contributed a Git pull request to the ngCordova project for Ionic Framework. It can be found here:

    https://github.com/driftyco/ng-cordova/pull/658

    Phonegap and Apache Cordova users will still have to use cordova.exec, but Ionic users who include ngCordova will be able to do:

  • jcesar

    It returns 0 on iOS

    • What version of iOS are you using and are you using a device or simulator? Also please confirm that you are not using the Ionic View, ionic serve, or live-reload in your setup.

      Regards,

      • jcesar

        It’s a known issue and it’s been there for almost a year, but it hasn’t been fixed because getFreeDiskSpace is undocumented and shouldn’t be used. It happens on simulator and device on a regular app

        • That is weird. I could have sworn it worked when I tested in iOS.

          Your iOS version would still be helpful in my investigation.

          Regards,

  • Ifeora Okechukwu

    Nice tip…. Thanks!!!