My phone is rooted. I'm trying to do a very simple program. The program should delete file from app/app folder. How can I do this? I'm newbie, so example code is valued.
3 Answers
If your phone is rooted, you can issue commands as root through su
—provided that the su
binary is present and in your PATH
—since Android is a variant of Linux. Simply execute the delete commands through Runtime.exec()
, and Superuser should take care of the permission prompt.
Here's a simple example of its usage I took from this question:
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();

- 1
- 1

- 37,593
- 16
- 136
- 187
You can delete all files inside a folder recursively using the below method.
private void DeleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
{
child.delete();
DeleteRecursive(child);
}
fileOrDirectory.delete();
}

- 20,897
- 15
- 57
- 78
-
Even in a rooted phone? I thought once your phone is rooted, you can do whatever you like. – Lazy Ninja Nov 21 '12 at 09:07
-
This won't work for system directories, because your app is sandboxed and isn't allowed access to it. – Paul Lammertsma Nov 21 '12 at 09:09
On his github, Chainfire provides a sample implementation of a Shell
class that you can use to execute the rm
command as root. The rm
command is the Linux variant of the command to delete files (and folders).
Code Snippet:
if(Shell.SU.available()){
Shell.SU.run("rm /data/app/app.folder.here/fileToDelete.xml"); //Delete command
else{
System.out.println("su not found");
Or if you are certain that the su
binary is available, you can just run the delete command (commented line) and skip the check
Source: How-To SU

- 454
- 1
- 3
- 14