This library provides the
GroupsDelete API that allows you to delete existing Groups.
An instance of the
GroupsDelete API is obtained by,
val delete = Contacts(context).groups().delete()
To delete a set of existing groups,
val deleteResult = Contacts(context) .groups() .delete() ?.groups(existingGroups) ?.commit()
To execute the delete,
If you want to delete all given groups in a single atomic transaction,
The call to
commitInOneTransaction will only succeed if ALL given groups are successfully deleted.
If one delete fails, the entire operation will fail and everything will be reverted prior to the
delete operation. In contrast,
commit allows for some deletes to succeed and some to fail.
commitInOneTransaction functions returns a
To check if all deletes succeeded,
val allDeletesSuccessful = deleteResult.isSuccessful
To check if a particular delete succeeded,
val firstDeleteSuccessful = deleteResult.isSuccessful(group1)
Deletes are executed when the
commitInOneTransaction function is invoked. The work is
done in the same thread as the call-site. This may result in a choppy UI.
To perform the work in a different thread, use the Kotlin coroutine extensions provided in the
For more info, read How do I use the async module to simplify executing work outside of the UI thread using coroutines?
You may, of course, use other multi-threading libraries or just do it yourself =)
Extensions for Kotlin Flow and RxJava are also in the v1 roadmap.
Deletes require the
android.permission.WRITE_CONTACTS permissions. If not granted, the delete
will do nothing and return a failed result.
To perform the delete with permission, use the extensions provided in the
For more info, read How do I use the permissions module to simplify permission handling using coroutines?
You may, of course, use other permission handling libraries or just do it yourself =)
Prior to Android 8.0 (Oreo, API 26), group deletion is unpredictable. Groups that are marked for deletion remain in the DB and is still shown in the native Contacts app. Sometimes they do get deleted at some point but the trigger for the actual deletion eludes me.
The native Contacts app (prior to API 26) does NOT support group deletion perhaps because groups syncing isn’t implemented or at least not to the same extent as contacts syncing. Therefore, this library will also not support group deletion for API versions lower than 26.
Groups created by the system are typically read-only. You cannot delete them, even if you try! The Contacts Provider typically have the following system groups (for standard Google Accounts),
The above list may vary per account.
GroupsDelete API will not attempt to delete a read-only group and will simply result in
When a group is deleted, any membership to that group is deleted automatically by the Contacts Provider.
Groups are not immediately deleted. However, they are marked for deletion and they do get deleted in the background by the Contacts Provider depending on sync settings.
However, group memberships to those groups marked for deletion are immediately deleted!