148

Using adb shell we can clear application data.

adb shell pm clear com.android.browser

But when executing that command from the application

String deleteCmd = "pm clear com.android.browser";      
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(deleteCmd);
        } catch (IOException e) {
            e.printStackTrace();                
        }

Issue:

It doesn't clear the user data nor give any exception though I have given the following permission.

<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA"/>

Question:

How to clear another app application data using adb shell?

UdayaLakmal
  • 4,035
  • 4
  • 29
  • 40

8 Answers8

291

This command worked for me:

adb shell pm clear packageName
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Manmohan Soni
  • 6,472
  • 2
  • 23
  • 29
8

Afaik the Browser application data is NOT clearable for other apps, since it is store in private_mode. So executing this command could probalby only work on rooted devices. Otherwise you should try another approach.

Thkru
  • 4,218
  • 2
  • 18
  • 37
  • 1
    The command line tools are not intdended for usage by applications. Likely they will only work for the semi-privileged adb shell user, which is treated as somewhat comparable in authority to a user pushing buttons in GUI if the system settings app. – Chris Stratton Jun 07 '12 at 15:10
6

The command pm clear com.android.browser requires root permission.
So, run su first.

Here is the sample code:

private static final String CHARSET_NAME = "UTF-8";
String cmd = "pm clear com.android.browser";

ProcessBuilder pb = new ProcessBuilder().redirectErrorStream(true).command("su");
Process p = pb.start();

// We must handle the result stream in another Thread first
StreamReader stdoutReader = new StreamReader(p.getInputStream(), CHARSET_NAME);
stdoutReader.start();

out = p.getOutputStream();
out.write((cmd + "\n").getBytes(CHARSET_NAME));
out.write(("exit" + "\n").getBytes(CHARSET_NAME));
out.flush();

p.waitFor();
String result = stdoutReader.getResult();

The class StreamReader:

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.CountDownLatch;

class StreamReader extends Thread {
    private InputStream is;
    private StringBuffer mBuffer;
    private String mCharset;
    private CountDownLatch mCountDownLatch;

    StreamReader(InputStream is, String charset) {
        this.is = is;
        mCharset = charset;
        mBuffer = new StringBuffer("");
        mCountDownLatch = new CountDownLatch(1);
    }

    String getResult() {
        try {
            mCountDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return mBuffer.toString();
    }

    @Override
    public void run() {
        InputStreamReader isr = null;
        try {
            isr = new InputStreamReader(is, mCharset);
            int c = -1;
            while ((c = isr.read()) != -1) {
                mBuffer.append((char) c);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (isr != null)
                    isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mCountDownLatch.countDown();
        }
    }
}
fantouch
  • 1,159
  • 14
  • 12
  • Android 8.1 MiUi 10 Root I cannot clear any 1 application even with ADB SHELL with SU parameter – anarbus Sep 09 '20 at 20:27
3

To clear Application Data Please Try this way.

    public void clearApplicationData() {
    File cache = getCacheDir();
    File appDir = new File(cache.getParent());
    if (appDir.exists()) {
        String[] children = appDir.list();
        for (String s : children) {
            if (!s.equals("lib")) {
                deleteDir(new File(appDir, s));Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
            }
        }
    }
}

public static boolean deleteDir(File dir) {
    if (dir != null &amp;&amp; dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    return dir.delete();
}
Md Abdul Gafur
  • 6,213
  • 2
  • 27
  • 37
  • i try this,using this can clear our own application cache data, i need to clear another app user data from our application – UdayaLakmal Jun 07 '12 at 15:10
  • 3
    @UdayaLakmal you are not supposed to be able to clear another application's cache data, because android is designed to protect applications from potentially malicious actions by other applications. – Chris Stratton Jun 07 '12 at 15:11
3

To clear the cache for all installed apps:

  • use adb shell to get into device shell ..
  • run the following command : cmd package list packages|cut -d":" -f2|while read package ;do pm clear $package;done
Bash Stack
  • 509
  • 3
  • 12
2

On mac you can clear the app data using this command

adb shell pm clear com.example.healitia

enter image description here

1

To reset/clear application data on Android, you need to check available packages installed on your Android device-

  • Go to adb shell by running adb shell on terminal
  • Check available packages by running pm list packages
  • If package name is available which you want to reset, then run pm clear packageName by replacing packageName with the package name which you want to reset, and same is showing on pm list packages result.

If package name isn't showing, and you will try to reset, you will get Failed status.

-2
// To delete all the folders and files within folders recursively
File sdDir = new File(sdPath);

if(sdDir.exists())
    deleteRecursive(sdDir);




// Delete any folder on a device if exists
void deleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory())
    for (File child : fileOrDirectory.listFiles())
        deleteRecursive(child);

    fileOrDirectory.delete();
}
sjain
  • 23,126
  • 28
  • 107
  • 185
  • 2
    This would (if used with appropriate permission) delete the shared files on the External Storage without regard to app association. It **will not** delete the private files of any app, which is what the question was about. – Chris Stratton Mar 09 '15 at 21:16