This library provides the
DataDelete API that allows you to delete a list of any data kinds
directly without having to delete them via Contacts/RawContacts.
An instance of the
DataDelete API is obtained by,
val delete = Contacts(context).data().delete()
To delete all kinds of data via Contacts/RawContacts, you may remove them from the Contact/RawContact and then perform an update. For more info, read How do I update contacts?
To delete a set of data,
val deleteResult = Contacts(context) .data() .delete() .data(data) .commit()
If you want to delete a list of emails and phones,
val deleteResult = Contacts(context) .data() .delete() .data(emails + phones) .commit()
To execute the delete,
If you want to delete all given data in a single atomic transaction,
The call to
commitInOneTransaction will only succeed if ALL given data 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(data1)
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 =)
DataDelete API also supports deleting the Profile (device owner) contact data. To get an
instance of this API for Profile data deletes,
val profileDataDelete = Contacts(context).profile().data().delete()
All deletes will be limited to the Profile, whether it exists or not.
DataDelete API supports custom data. For more info, read How do I use delete APIs to delete custom data?