Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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:
http://androidheadlines.com/images/android-how-and-when-to-clear-app-cache-or-data_waiod_0.jpg

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

— asked October 18th 2011 by Tuan Nguyen
  • android
  • clear
  • clear data
  • data
  • mobile
0 Comments

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
    if(dataDirectory.exists()){
    
        //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]);
    
            if(fileToDelete.exists()){
                fileToDelete.deleteFile();
            }    
        }
    
    }
    
    — answered October 19th 2011 by James Timberlake
    permalink
    5 Comments
    • Thank you, your response seems to be what I am looking for.

      — commented October 19th 2011 by Tuan Nguyen
    • nothing happens.. it takes data from the cache only even after using the above code.

      — commented December 28th 2012 by Dhananjay Choudhari
    • cache is different from applicationdatadirectory. to clear the cache you must empty the Ti.Filesystem.applicationCacheDirectory instead of the applicationDataDirectory

      — commented November 6th 2013 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 May 13th 2014 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 August 21st 2014 by Caio Almeida
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.