2

How can I programmatically delete data or format the entire SD Card?

skink
  • 5,133
  • 6
  • 37
  • 58
user583267
  • 31
  • 1
  • 2
  • 2
    I sort of hope this ISN'T possible. Why would an app ever need to do this? What happens if your application is stored on the SD card? – Kevin Coppock Jan 20 '11 at 16:50

3 Answers3

5

code to wipe SDCARD

public void wipingSdcard() {
        File deleteMatchingFile = new File(Environment
                .getExternalStorageDirectory().toString());
        try {
            File[] filenames = deleteMatchingFile.listFiles();
            if (filenames != null && filenames.length > 0) {
                for (File tempFile : filenames) {
                    if (tempFile.isDirectory()) {
                        wipeDirectory(tempFile.toString());
                        tempFile.delete();
                    } else {
                        tempFile.delete();
                    }
                }
            } else {
                deleteMatchingFile.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void wipeDirectory(String name) {
        File directoryFile = new File(name);
        File[] filenames = directoryFile.listFiles();
        if (filenames != null && filenames.length > 0) {
            for (File tempFile : filenames) {
                if (tempFile.isDirectory()) {
                    wipeDirectory(tempFile.toString());
                    tempFile.delete();
                } else {
                    tempFile.delete();
                }
            }
        } else {
            directoryFile.delete();
        }
    }
Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
0

Try reading up on this one How to delete a file from SD card?

Community
  • 1
  • 1
BadSkillz
  • 1,993
  • 19
  • 37
0

By using the File class you should be able to list all the files on the SDCard and to delete each of them one by one. You'll make a recursive function to delete directories. It is however not such a good idea and the OS might prevent you from deleting some of the files/folders used by the system or another user.

Vincent Mimoun-Prat
  • 28,208
  • 16
  • 81
  • 124