3

There is a command to ease the pain of managing packages for Android phone,

adb shell pm uninstall org.kde.necessitas.example.one
adb shell pm uninstall org.kde.necessitas.example.two

But I have many phones and just want to delete all packages from a particular domain on them.

It cannot be done by

adb shell pm uninstall org.kde.necessitas.example.*

what is your suggestion?

Saeed
  • 550
  • 1
  • 7
  • 12

3 Answers3

4

You can use the following in a batch file: (I am assuming Windows though)

  adb shell pm list packages org.kde.necessitas.example > packages.txt

  for /F "tokens=2 delims=:" %%a in (packages.txt) do adb shell pm uninstall %%a

You could take it a step further and make the search text a parameter:

  adb shell pm list packages %1 > packages.txt

  for /F "tokens=2 delims=:" %%a in (packages.txt) do adb shell pm uninstall %%a

This pipes the output of the pm list packages command into a text file and then loops through each line of the text file. It calls adb shell pm uninstall for each second token in the line which in this case is the package name.

Simon
  • 14,407
  • 8
  • 46
  • 61
  • This actually is a valid approach, but I was wondering if there is an easier/better approach. – Saeed Mar 17 '13 at 11:58
  • no need to use `find` - just do `pm list packages org.kde.necessitas.example`. also the `pm list` command prints out the package names in format `package:com.example.package.name` - you need to remove the `package:` part before you use it – Alex P. Mar 17 '13 at 18:20
  • @AlexP. Thanks Alex, I've edited. Good point about not needing Find. – Simon Mar 17 '13 at 18:51
  • Why do we have to save the list to the file? Why can't we pipe the list? I have tired `adb shell pm list packages com.ubaier.test | cut -d ':' -f 2 | xargs -L1 adb uninstall` .. but it does not work :( .. I just want to understand why? – Ubaier Bhat Jul 07 '17 at 13:06
1

If you're sat in a shell on the phone itself, you can do this (all on one line, if you want):

for i in $(pm list packages com.your.domain ) ; do
    pm uninstall ${i#*:} ;
done

If you're on the host machine and you're using something Unixy - Linux, Mac, Cygwin - then something similar will work there too, but you need to shove 'adb shell' in:

for i in $(adb shell pm list packages com.your.domain ) ; do
    adb uninstall ${i#*:} ;
done

Since you're talking about removing the packages from all connected phones, you need yet another loop:

for d in $(adb devices | sed '/List/d; /\*/d; s/device$//') ; do
    for i in $(adb -s $d shell pm list packages com.your.domain ) ; do
        adb -s $d uninstall ${i#*:} ;
    done
done
android.weasel
  • 3,343
  • 1
  • 30
  • 41
  • This answer helped me, although I had to first correct the parameter expansion enclosing braces by changing `$(i#*:}` instances to `${i#*:}` ([I suggested an edit but it was rejected](https://stackoverflow.com/review/suggested-edits/19872988)) – AndreCunha Jun 03 '18 at 00:15
  • For some reason this is not working on devices with Android Lollipop on it (like Samsung Note 3). Any workaround you might be aware of? The error simply comes up as Failure [DELETE_FAILED_INTERNAL_ERROR]. – Shredder Apr 21 '19 at 17:58
1

for mac users:

adb shell pm list packages com.your.domain \
| cut -d ':' -f 2 \
| tr -d '\r' \
| xargs -L1 -t adb uninstall
Ubaier Bhat
  • 854
  • 13
  • 33
  • just use the 1st option in @android.weasel's answer. it's platform independent (the whole command runs inside `adb shell`). – Alex P. Jul 08 '17 at 21:25