9

I am trying to create a script that will retrieve and uninstall all user apps in one batch operation through adb. Does anyone know how I would be able to do this?

I can currently list out all 3rd party apps through

adb shell pm list packages -3

Could I somehow direct the list of packages this generates into an uninstall command in adb?

Alex P.
  • 30,437
  • 17
  • 118
  • 169
Troy Greenman
  • 111
  • 1
  • 6

2 Answers2

23

Try:

adb shell pm list packages -3 | cut -d':' -f2 | tr '\r' ' ' | xargs -r -n1 -t adb uninstall
  • First part is to get 3rd party app package names
  • second part is to split package names using delimiter :
  • third part is to replace carriage return with space (sometimes it would be a problem in linux machines. Try removing this part and check if you face it)
  • and the last one is for uninstalling one by one
    • r option will prevent xargs from running the command if there are no third party apps installed
    • n1 option is to pass one result value as argument at a time to the command
    • t is for printing the command being executed)

Hope it helps!!!

Ramraj
  • 2,040
  • 14
  • 17
  • I have executed the above command. It is telling 'cut' in not recognized as internal or external command, operable program or batch file. Please tell me where i am failing. My adb is up and running and it it is listing the device serial id. – Humble_PrOgRaMeR Jun 19 '17 at 12:44
  • cut is a linux command. It seems that you are running on Windows. – Ramraj Jun 20 '17 at 17:41
  • Write the output of adb command to a file and use a 'for loop' to cut and uninstall. ex: https://stackoverflow.com/a/4442121/3604656 – Ramraj Jun 20 '17 at 17:52
  • But, in my another Windows machine same command is working. Is there any difference here?? Both windows machines are of same configuration. – Humble_PrOgRaMeR Jun 23 '17 at 14:07
  • Are you using command prompts that supports GNU tools..? Like Cygwin or git bash in the other windows system.. – Ramraj Jul 13 '17 at 10:39
  • 2
    Mac OS X Terminal gives me: "xargs: illegal option -- r". I tried removing the 3rd section, which changed the error to: "xargs: replacements must be a number". I believe my ADB is set up correctly. – phpN00b Jul 15 '18 at 04:35
  • Please replace "xargs -r" with "xargs -I" in Mac . Ref: https://unix.stackexchange.com/a/285170 – Ramraj Jul 19 '18 at 12:10
2

What seems to be the problem? It can be done with this one-liner:

adb shell "pm list packages -3 | cut -c9- | xargs pm uninstall"
Alex P.
  • 30,437
  • 17
  • 118
  • 169