First, you can query all your contacts:
$ adb shell content query --uri content://com.android.contacts/contacts
Row: 0 last_time_contacted=0, phonetic_name=NULL, custom_ringtone=NULL, contact_status_ts=NULL, pinned=0, photo_id=NULL, photo_file_id=NULL, contact_status_res_package=NULL, contact_chat_capability=NULL, contact_status_icon=NULL, display_name_alt=Doe, John, sort_key_alt=Doe, John, in_visible_group=0, starred=0, contact_status_label=NULL, phonebook_label=S, is_user_profile=0, has_phone_number=1, display_name_source=40, phonetic_name_style=0, send_to_voicemail=0, lookup=4073r100-4D5331434D4F31394337453333, phonebook_label_alt=S, contact_last_updated_timestamp=1571690183031, photo_uri=NULL, phonebook_bucket=19, contact_status=NULL, display_name=John Doe, sort_key=John Doe, photo_thumb_uri=NULL, contact_presence=NULL, in_default_directory=1, times_contacted=0, _id=100, name_raw_contact_id=100, phonebook_bucket_alt=19
Row: 1 last_time_contacted=0, phonetic_name=NULL, custom_ringtone=NULL, contact_status_ts=NULL, pinned=0, photo_id=NULL, photo_file_id=NULL, contact_status_res_package=NULL, contact_chat_capability=NULL, contact_status_icon=NULL, display_name_alt=Doe, Jane, sort_key_alt=Doe, Jane, in_visible_group=0, starred=0, contact_status_label=NULL, phonebook_label=C, is_user_profile=0, has_phone_number=1, display_name_source=40, phonetic_name_style=0, send_to_voicemail=0, lookup=4073r101-2D374B394D4F3929432B512B312D3D, phonebook_label_alt=B, contact_last_updated_timestamp=1571690183031, photo_uri=NULL, phonebook_bucket=3, contact_status=NULL, display_name=Jane Doe, sort_key=Jane Doe, photo_thumb_uri=NULL, contact_presence=NULL, in_default_directory=1, times_contacted=0, _id=101, name_raw_contact_id=101, phonebook_bucket_alt=2
Row: 2 last_time_contacted=0, phonetic_name=NULL, custom_ringtone=NULL, contact_status_ts=NULL, pinned=0, photo_id=NULL, photo_file_id=NULL, contact_status_res_package=NULL, contact_chat_capability=NULL, contact_status_icon=NULL, display_name_alt=Roe, Jane, sort_key_alt=Roe, Jane, in_visible_group=0, starred=0, contact_status_label=NULL, phonebook_label=A, is_user_profile=0, has_phone_number=1, display_name_source=40, phonetic_name_style=0, send_to_voicemail=0, lookup=4073r102-29432F4B31294D55393F3F, phonebook_label_alt=W, contact_last_updated_timestamp=1571690183031, photo_uri=NULL, phonebook_bucket=1, contact_status=NULL, display_name=Jane Roe, sort_key=Jane Roe, photo_thumb_uri=NULL, contact_presence=NULL, in_default_directory=1, times_contacted=0, _id=102, name_raw_contact_id=102, phonebook_bucket_alt=23
Row: 3 last_time_contacted=0, phonetic_name=NULL, custom_ringtone=NULL, contact_status_ts=NULL, pinned=0, photo_id=NULL, photo_file_id=NULL, contact_status_res_package=NULL, contact_chat_capability=NULL, contact_status_icon=NULL, display_name_alt=Roe, Richard, sort_key_alt=Roe, Richard, in_visible_group=0, starred=0, contact_status_label=NULL, phonebook_label=U, is_user_profile=0, has_phone_number=1, display_name_source=40, phonetic_name_style=0, send_to_voicemail=0, lookup=4073r103-51432B313D2943434F2B4B31315B31, phonebook_label_alt=B, contact_last_updated_timestamp=1571690183031, photo_uri=NULL, phonebook_bucket=21, contact_status=NULL, display_name=Richard Roe, sort_key=Richard Roe, photo_thumb_uri=NULL, contact_presence=NULL, in_default_directory=1, times_contacted=0, _id=103, name_raw_contact_id=103, phonebook_bucket_alt=2
You can use sed
to, e.g., select the first three rows and trim them to their ids (i.e., the _id
attribute:
$ adb shell content query --uri content://com.android.contacts/contacts | sed -n '1,3 s/^.* _id=\([[:digit:]]\+\),.*$/\1/p'
100
101
102
This sed
command says not to print anything by default (the -n
switch), but for lines 1 to 3 substitute the lines with the value of the _id
and print the result (the p
flag at the end of the substitution).
I now tried to loop over this output and issue an adb shell delete
for every id, but this does not work. It will only issue a command for the first id. I don’t know exactly why this is, but it must have something to do with what the adb shell
command does.
As a workaround, we can do all this directly in the adb shell. So, first execute adb shell
and then in that shell you can directly use the content
command. For better readability I first save the ids in a file and then loop over its lines. Of course, it’s perfectly possible to just pipe the output of sed into the while loop.
mycomputer$ adb shell
android$ content query --uri content://com.android.contacts/contacts | sed -n '1,3 s/^.* _id=\([[:digit:]]\+\),.*$/\1/p' >ids
android$ while read -r id; do content delete --uri content://com.android.contacts/contacts/"$id"; done <ids