16

Is it possible to kill ALL the active tasks/apps in the task manager using ADB? This would be equivalent of opening the task manager and killing each task one by one...

I tried using the the following adb shell command but that didn't kill all the task.

adb shell am kill-all

I can't use the adb shell am force-stop <PACKAGE> command because it would require me to know which package/app is running. I want to kill ALL the user apps task that are running. Similarly to using the task manager and killing each task one by one.

According to the command description, kill-all kills all background processes. Are background processes equivalent to "services" and task equivalent to "activities"?

Also, is it possible to clear cache of apps using ADB while keeping the user data? I seems that the adb shell pm clear clears all the user data. I want to only clear the cache.

The reason why I am asking is because I am doing some performance testing on few user apps. To make each test valid, I want to ensure none of the user apps have any task, activities, services, and cache already in the background.

Zythyr
  • 1,142
  • 4
  • 20
  • 33
  • 1
    Possible duplicate of [this](http://stackoverflow.com/questions/3117095/stopping-an-android-app-from-console) post. – Gustavo Morales Jun 26 '15 at 18:36
  • I do not believe this is a duplicate. The other SO question is in regards to closing and clearing one application from the device where this question is about removing all non essential running tasks/apps. – running-codebase Jun 16 '16 at 00:44

4 Answers4

15

You can use force-stop, it doesn't require root permission.

adb shell am force-stop <PACKAGE>

And you can get the package name from the top running activity/app

adb shell "dumpsys activity | grep top-activity"

After that you need to play a bit with the result, to extract the package, here my java code that does that:

public void parseResult(String line){
        int i = line.indexOf(" 0 ");
        if(i == -1){
            return;
        }
        line = line.substring(i);
        i = line.indexOf(":");
        if(i == -1){
            return;
        }
        line = line.substring(i + 1);
        i = line.indexOf("/");
        return line.substring(0, i);
}
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • better way is `line.substring(line.lastIndexOf(":") + 1, line.lastIndexOf("/"));`. Output is like `Proc # 0: fore T/A/T trm: 0 3184:com.android.settings/1000 (top-activity)` – Vlad Aug 29 '17 at 11:50
  • I made 1 small change to ^ command to get it to show a list of running apps. I updated "top-activity" to "topActivity" and then it worked – Austin Dec 21 '22 at 03:32
8

If you want to start clean slate i.e close the app and clear its data too you can do the following

adb shell pm clear com.yourapp.package
Nishant Srivastava
  • 4,723
  • 2
  • 24
  • 35
  • i didnot see the post @Gustavo mentioned, but i guess its the same. – Nishant Srivastava Jun 26 '15 at 18:37
  • 3
    'adb shell pm clear' removes all the user data. I want to keep the user data while only clearing the cache of the user app. – Zythyr Jun 26 '15 at 18:38
  • To clear the cache you need root privileges. If you do have that you can access the file system and clear the cache folder for your particular app – Nishant Srivastava Jun 26 '15 at 18:44
  • Yes, I have root privileges. How can I clear the cache using ADB? Also, do I have to do it for each app/package one by one? Can't I just it for all the packages at once? If I have to each it for each package one by one, how can I programmatically get names of each user app installed and clear the cache. – Zythyr Jun 26 '15 at 18:47
2

For non rooted devices I expanded on Faisal Ameer's Script

adb shell ps | grep -v root | grep -v system | grep -v "android.process." | grep -v radio | grep -v "com.google.process." | grep -v "com.lge." | grep -v shell | grep -v NAME | awk '{print $NF}' | tr '\r' ' ' | xargs adb shell am force-stop

The adb shell am force-stop does not require root permission. Note that the applications still show up in the devices running application drawer but I have verified that the packages processes have been cleared using.

adb shell dumpsys meminfo relevant.package.names
running-codebase
  • 998
  • 2
  • 12
  • 17
1

find running apps, ignoring system apps etc and kill, the following command does it all;

adb shell ps|grep -v root|grep -v system|grep -v NAME|grep -v shell|grep -v smartcard|grep -v androidshmservice|grep -v bluetooth|grep -v radio|grep -v nfc|grep -v "com.android."|grep -v "android.process."|grep -v "com.google.android."|grep -v "com.sec.android."|grep -v "com.google.process."|grep -v "com.samsung.android."|grep -v "com.smlds" |awk '{print $2}'| xargs adb shell kill

you can add more exceptions if you find any like this; grep -v "exception"

Faisal Ameer
  • 416
  • 5
  • 13