How to delete internal storage file in android? How to delete internal storage file in android? android android

How to delete internal storage file in android?


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


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.


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