How to delete the internal storage file in android?

I used the Android internal storage to save the file for my application (using openFileOutput ), but I would like to delete this file, is it possible and how?

+32
android file-io
Aug 24 '10 at 8:32
source share
7 answers
 File dir = getFilesDir(); File file = new File(dir, "my_filename"); boolean deleted = file.delete(); 
+71
Aug 24 '10 at 9:06
source share

I know this is a bit like the old one, but the docs say:

 deleteFile("filename"); 

but not:

 File.delete(); 

What if you are already using:

 getFilesDir(); 

has the meaning.

+17
Nov 21 '12 at 9:21
source share

You should always delete files that you no longer need. The easiest way to delete a file is to open an open call to the delete () file link.

myFile.delete() ;

If the file is saved in internal memory, you can also ask the context to find and delete the file by calling deleteFile ():

myContext.deleteFile(fileName);

Note. When a user uninstalls your application, the Android system deletes the following: All files stored on the external storage All files stored on the external storage using getExternalFilesDir() . However, you must manually delete all cached files created with getCacheDir() on a regular basis, as well as regularly delete other files that you no longer need.

Source : http://developer.android.com/training/basics/data-storage/files.html

+3
Nov 20 '14 at 19:04
source share

Use delete file method

+1
Aug 24 '10 at 8:37
source share
 new File(mUri.toString).delete(); 
+1
Jul 28 2018-11-17T00:
source share

If you want to delete all files from a folder, use the following function:

 private void deleteTempFolder(String dir) { File myDir = new File(Environment.getExternalStorageDirectory() + "/"+dir); if (myDir.isDirectory()) { String[] children = myDir.list(); for (int i = 0; i < children.length; i++) { new File(myDir, children[i]).delete(); } } } 

The folder must be present in storage. If not, we can check another coding for it.

  if (myDir.exists() && myDir.isDirectory()) { //write same defination for it. } 
+1
Nov 19 '16 at 20:11
source share
  void clearMyFiles() { File[] files = context.getFilesDir().listFiles(); if(files != null) for(File file : files) { file.delete(); } } 
0
Aug 6 '17 at 17:07 on
source share



All Articles