clear stored data

William Hunter

Active Member
Licensed User
Longtime User
Is there a command that would delete stored application data? I've searched the Forum without success. Any advice on this would be greatly appreciated.
:sign0085:
 
You can create a list of the files in the directory you want to delete then use a for loop to go through the list and delete them.
 
Upvote 0
You can create a list of the files in the directory you want to delete then use a for loop to go through the list and delete them.
Thank you for your reply. I'm hoping to find something more generic than deleting a list of files. Something like flushing a cache, but instead clearing stored data. Do you or anyone else have any other suggestions?
:sign0163:
 
Upvote 0
I don't know how good you is in java but take a look at this:
How to clear user data programmatically in java

Code: Copy
package com.hrupin.cleaner;

import java.io.File;

import android.app.Application;
import android.util.Log;

public class MyApplication extends Application {
   private static MyApplication instance;

   @Override
   public void onCreate() {
      super.onCreate();
      instance = this;
   }

   public static MyApplication getInstance() {
      return instance;
   }

   public void clearApplicationData() {
      File cache = getCacheDir();
      File appDir = new File(cache.getParent());
      if (appDir.exists()) {
         String[] children = appDir.list();
         for (String s : children) {
            if (!s.equals("lib")) {
               deleteDir(new File(appDir, s));
               Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
            }
         }
      }
   }

   public static boolean deleteDir(File dir) {
      if (dir != null && dir.isDirectory()) {
         String[] children = dir.list();
         for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
               return false;
            }
         }
      }

      return dir.delete();
   }
}
 
Upvote 0
Clear store data programmatically

@ Pluton – This is exactly what I’m looking for. Unfortunately, my one and only try at working with Eclipse led me to the conclusion that I’m programmatically challenged. I will take a serious look at this, to see if I can better understand what has to be done. It would make a very useful library for use with B4A.

@ jonPM – There may be a way to duplicate this function in pure B4A. I’m new to B4A and don’t yet have the knowledge to write the code. As I understand it, by default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). I’m not sure how to go about this and would likely end up deleting the entire contents of the application directory. I was hoping someone had gone down this road before, and had a code sample to share.

Thank you both for your responses.
Regards :)
 
Upvote 0
What I meant is that Pluton's java code does exactly what I've described. It looks in the apps directory, makes a list of the files in it, then deletes them one by one. You don't need a library to do this.
 
Upvote 0
What I meant is that Pluton's java code does exactly what I've described. It looks in the apps directory, makes a list of the files in it, then deletes them one by one. You don't need a library to do this.
Hello jonPM – I could do something like the code below, but I would have to know where the files were stored. I would need some means of determining if the files were in DirInternal or DirExternal, and I don’t know how to do this. At this point, it’s the details that are eluding me. Does this seem like a reasonable way to start?

Regards
Code: Copy
Dim FilesToDelete As List

FilesToDelete.Initialize
FilesToDelete.AddAll(File.ListFiles(File.DirInternal))

For I = 0 To FilesToDelete.Size -1
    
    File.Delete(File.DirInternal, FilesToDelete.Get(I))

Next
 
Upvote 0
Yes, that's what you need to do. You should know where the files are since you are writing them elsewhere in your code. If you are using multiple locations then just repeat the code accordingly.
 
Upvote 0
Yes, that's what you need to do. You should know where the files are since you are writing them elsewhere in your code. If you are using multiple locations then just repeat the code accordingly.
Thank you jonPM - Depending on the resources available, my app will install directly to the device or an sd card. This is why I need to verify if an sd card exists. The code below is what I have come up with, but I get the error “Unknown member: externalwriteable” at line: If File.ExternalWriteable = True Then. Do you know if a library is required to use this means of verifying an sd card?

Cheers
Code: Copy
Sub ClearStoredData

Dim DefaultDir As String
Dim FilesToDelete As List

If File.ExternalWriteable = True Then
   DefaultDir = "DirExternal"
Else
   DefaultDir = "DirInternal"
End If

FilesToDelete.Initialize
FilesToDelete.AddAll(File.ListFiles(File.DefaultDir))

For I = 0 To FilesToDelete.Size -1
   File.Delete(File.DefaultDir, FilesToDelete.Get(I))
Next

End Sub
 
Upvote 0
That line looks correct, but you can't add a member to File (with DefaultDir) in that manner. instead do this:

Code: Copy
Sub ClearStoredData

Dim DefaultDir As String
Dim FilesToDelete As List

If File.ExternalWriteable = True Then
    DefaultDir = File.DirDefaultExternal
Else
    DefaultDir = File.DirInternal
End If

FilesToDelete.Initialize
FilesToDelete.AddAll(File.ListFiles(DefaultDir))

For I = 0 To FilesToDelete.Size -1
    File.Delete(DefaultDir, FilesToDelete.Get(I))
Next

End Sub
 
Upvote 0
That line looks correct, but you can't add a member to File (with DefaultDir) in that manner. instead do this:

Code: Copy
Sub ClearStoredData

Dim DefaultDir As String
Dim FilesToDelete As List

If File.ExternalWriteable = True Then
    DefaultDir = File.DirDefaultExternal
Else
    DefaultDir = File.DirInternal
End If

FilesToDelete.Initialize
FilesToDelete.AddAll(File.ListFiles(DefaultDir))

For I = 0 To FilesToDelete.Size -1
    File.Delete(DefaultDir, FilesToDelete.Get(I))
Next

End Sub
Thank you jonMP - you have been most helpful. I now have things sorted out. For anyone interested in this post, I had a typo that generated the “Unknown member: externalwriteable” error. I incorrectly spelled Writable as Writeable. Silly me! :sign0161:

Regards
 
Upvote 0
Back
Top