Android - Clear Data Programmatically

Is it possible to clear the data for an Android application programmatically?

I am referring to the clear data functionality found in this image when to go to an application's settings on an Android device:

Is there a function in Titanium that allows the data on an Android application to be cleared?

1 Answer

  • Accepted Answer

    Yes you can. You can access the data files through Titanium.Filesystem.applicationDataDirectory to find all the files listed in the directory and then you can delete them one by one. It would probably look something like this:

    //@author jtimberlake
    //get the data directory
    var directory = Ti.Filesystem.applicationDataDirectory;
    var dataDirectory = Ti.Filesystem.getFile(directory);
    var data = [];
    //I always make checks like this because you never know
        //get all sub-directories and file paths in your directory
        data = dataDirectory.getDirectoryListing();
        //if the files still exist, destroy
        for( var i = 0; i < data.length; i++){
            var fileToDelete = Ti.Filesystem.getFile(directory,data[i]);
    — answered 4 years ago by James Timberlake
    • Thank you, your response seems to be what I am looking for.

      — commented 4 years ago by Tuan Nguyen
    • nothing happens.. it takes data from the cache only even after using the above code.

      — commented 3 years ago by Dhananjay Choudhari
    • cache is different from applicationdatadirectory. to clear the cache you must empty the Ti.Filesystem.applicationCacheDirectory instead of the applicationDataDirectory

      — commented 2 years ago by Peter Beukema
    • Althought clearing/empty the applicationCacheDirectory the data is fetched from RAM because application is active. but that clear ram functionality is done in the "clear data" button functionality.

      — commented 2 years ago by ramesh babu
    • hello, im trying to delete the cache folder using deleteDirectory(true); it is returning true, but it deletes nothing.

      when i try to list the Ti.Filesystem.getApplicationCacheDirectory(). it returns empty

      any ideas?

      — commented a year ago by Caio Almeida