61

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

tshepang
  • 12,111
  • 21
  • 91
  • 136
maxsap
  • 2,971
  • 9
  • 44
  • 70

8 Answers8

131
File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();
plugmind
  • 7,926
  • 4
  • 34
  • 39
25

I know this is a bit of an oldie, but the docs say to use:

deleteFile("filename");

rather than:

File.delete();

Which if you are already using:

getFilesDir();

kind of makes sense.

Barry
  • 1,258
  • 3
  • 13
  • 27
9

You should always delete files that you no longer need. The most straightforward way to delete a file is to have the opened file reference call delete() on itself.

myFile.delete();

If the file is saved on internal storage, you can also ask the Context to locate and delete a file by calling deleteFile():

myContext.deleteFile(fileName);

Note: When the user uninstalls your app, the Android system deletes the following: All files you saved on internal storage All files you saved on external storage using getExternalFilesDir(). However, you should manually delete all cached files created with getCacheDir() on a regular basis and also regularly delete other files you no longer need.

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

Vipul Divyanshu
  • 196
  • 2
  • 6
8

If you want to delete all files from a folder then 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();
            }
        }
    }

Folder must be present on storage. If not we can check one more codition for it.

  if (myDir.exists() && myDir.isDirectory()) {
//write same defination for it.
}
Stephan
  • 15,704
  • 7
  • 48
  • 63
Kailas Bhakade
  • 1,902
  • 22
  • 27
  • 1
    This is a very good answer, but if you're like me not seeing anything under myDir.list(), make sure to ask for storage permission real-time! Just how you would ask for any other permission (i.e.: Camera), but pass in "WRITE_EXTERNAL_STORAGE". – KBog Sep 20 '22 at 04:24
4
new File(mUri.toString).delete(); 
dev mz
  • 397
  • 2
  • 3
  • 12
3
 void clearMyFiles() {
    File[] files = context.getFilesDir().listFiles();
    if(files != null)
        for(File file : files) {
           file.delete();
        }
 }
djdance
  • 3,110
  • 27
  • 33
  • 6
    This answer was flagged as low-quality because of its length and content. Suggest adding a description of what it does and how it answers the question. – Steve Piercy Aug 06 '17 at 18:48
2

Another alternative in Kotlin

val file: File = context.getFileStreamPath("file_name")
val deleted: Boolean = file.delete()
Nijat Ahmadli
  • 711
  • 6
  • 13
1

Use delete method of File

st0le
  • 33,375
  • 8
  • 89
  • 89