How to delete internal storage file in android?

AndroidFile Io

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();

Solution 2 - Android

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.

Solution 3 - Android

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

Solution 4 - Android

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.
}

Solution 5 - Android

new File(mUri.toString).delete(); 

Solution 6 - Android

 void clearMyFiles() {
    File[] files = context.getFilesDir().listFiles();
    if(files != null)
        for(File file : files) {
           file.delete();
        }
 }

Solution 7 - Android

Use delete method of File

Solution 8 - Android

Another alternative in Kotlin

val file: File = context.getFileStreamPath("file_name")
val deleted: Boolean = file.delete()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmaxsapView Question on Stackoverflow
Solution 1 - AndroidplugmindView Answer on Stackoverflow
Solution 2 - AndroidBarryView Answer on Stackoverflow
Solution 3 - AndroidVipul DivyanshuView Answer on Stackoverflow
Solution 4 - AndroidKailas BhakadeView Answer on Stackoverflow
Solution 5 - Androiddev mzView Answer on Stackoverflow
Solution 6 - AndroiddjdanceView Answer on Stackoverflow
Solution 7 - Androidst0leView Answer on Stackoverflow
Solution 8 - AndroidNijat AhmadliView Answer on Stackoverflow