22

I'm running some automated tests (with calabash-android) on an Android app and I need to programatically trigger a clear of the app's cache, but not user data.

I found that the adb shell pm clear solution is not adequate, since it clears user data (which includes login details).

Is there any way to achieve this externally to the app (i.e. without code changes)?

1 Answer 1

13

I've been exploring the /data/data/<app's package> folder, and found a cache folder inside, which contained the cached files for the app that I wanted to clear the cache of. Then from adb shell I deleted the contents, and the app's cache was reset.

Most probably you can run adb shell su -c "rm -rf /data/data/<app's package>/cache/*" to delete the cache only for the app (the app might have some custom caching, which isn't affected by this). It worked for me.

Edit: Of course, your device's adb must be set up to have root access (in Cyanogen, you can enable it).

Edit: @running-codebase pointed out in the comments, that if your application is compiled with a debug key, you can also use run-as command in adb's shell. So it would look like this: adb shell run-as <app's package> rm -rf /data/data/<app's package>/cache/* This method does not require root.

3
  • 2
    You can do adb shell run-as <app's package> and delete files without root if you built the app with a debug key. Commented Jun 21, 2016 at 8:40
  • 1
    Thanks, I discovered it since then, but I forgot to update this answer. I'll add soon. Commented Jun 21, 2016 at 12:22
  • 3
    The run-as command doesn't work for me on an Android 10 emulator. The command is interpreted by the "outer" shell (not running under app's user) and this shell doesn't have access to app's directory, therefore the glob star doesn't match anything. The command executes silently, but doesn't delete the cache contents. What does work is this: adb shell 'run-as com.ebay.mobile sh -c "rm -rf cache/*"' - it runs a shell under the app's user which performs the glob successfully Commented Jan 27, 2020 at 10:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.