32

I tried File.delete() but it doesn't work. How to delete a directory on SD card?

I'm working on Android 2.1.

Greenhorn
  • 769
  • 2
  • 8
  • 17
  • Possible duplicate of [How to delete a whole folder and content?](https://stackoverflow.com/questions/4943629/how-to-delete-a-whole-folder-and-content) – bummi Dec 04 '17 at 12:14

5 Answers5

99

You have to have all the directory empty before deleting the directory itself, see here

In Android, you should have the proper permissions as well - WRITE_EXTERNAL_STORAGE in your manifest.

EDIT: for convenience I copied the code here, but it is still from the link above

public static boolean deleteDirectory(File path) {
    if( path.exists() ) {
      File[] files = path.listFiles();
      if (files == null) {
          return true;
      }
      for(int i=0; i<files.length; i++) {
         if(files[i].isDirectory()) {
           deleteDirectory(files[i]);
         }
         else {
           files[i].delete();
         }
      }
    }
    return( path.delete() );
  }
nikib3ro
  • 20,366
  • 24
  • 120
  • 181
MByD
  • 135,866
  • 28
  • 264
  • 277
  • I tried removing files in the folder, but Logcat is showing `unable to unlink '/geo1.xml': Read-only file system (errno=30)`. I've added the `WRITE_EXTERNAL_STORAGE` permission in manifest. – Greenhorn Apr 18 '11 at 10:40
  • I think `/geo1.xml` is not in SD card, but in root (`/`). are you sure you are pointing to the correct file? – MByD Apr 18 '11 at 10:46
  • No, `/geo.xml` was in the folder I was trying to remove, but I was missing something in the code. Its working fine now. Thanks. :) – Greenhorn Apr 18 '11 at 10:55
  • 1
    You need to add a if (files != null) test around the for loop, or you'll throw an exception the first time the for loop is evaluated if files is null. – hemisphire Aug 17 '11 at 14:30
  • @ perfect,... perfect – user5716019 Feb 26 '16 at 08:31
  • 3
    Why do you return true if there are no files? That way the folder will not be deleted. – Apostrofix May 25 '16 at 11:44
  • This can be improved by replacing `for(int i=0; i – HB. Jul 08 '19 at 08:54
1

https://stackoverflow.com/a/16411911/2397275

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

in AndroidManifest.xml file

Community
  • 1
  • 1
Codeerror
  • 59
  • 1
  • 4
1

Directories must be empty before they will be deleted. You have to recursively empty and delete all directories in the tree:

boolean delete(File file) {
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files != null)
            for (File f : files) delete(f);
    }
    return file.delete();
}

Update:

It seems like file.isDirectory() == (file.listFiles() == null), but file.listFiles() logs "fail readDirectory() errno=20" when file.isDirectory() == false.

1

it's worked fine for me, i hope it will work for you.

File dir = new File(Environment.getExternalStorageDirectory()+"DirName"); 
if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            new File(dir, children[i]).delete();
        }
    }
Murali Mohan
  • 65
  • 2
  • 5
  • 2
    dir.list can return null, therefore calling children.length in your loop without checking whether children is null or not can throw an exception. – arash moeen Jul 01 '15 at 11:15
0

It worked for me:

Add in manifest-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

private boolean deleteDirectory(File path) {
        if( path.exists() ) {
            File[] files = path.listFiles();
            if (files == null) {
                return false;
            }
            for(File file : files) {
                if(file.isDirectory()) {
                    deleteDirectory(file);
                }
                else {
                file.delete();
                }
            }
        }
        return path.exists()?path.delete():false;
    }
Deven
  • 3,078
  • 1
  • 32
  • 34
  • Correct practice would be too replace `file.delete` with `boolean wasDeleted = file.delete();` and below that calling `if (wasDeleted) {Log.i("Deleted ", "successfully");}`. I also agree that it's better using a for each loop (`for(File file : files)`) instead of the accepted answer(`for(int i=0; i – HB. Jul 08 '19 at 09:00