How can I programmatically delete data or format the entire SD Card?
Asked
Active
Viewed 4,426 times
2
-
2I 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 Answers
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
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