Delete data
Stay organized with collections
Save and categorize content based on your preferences.
Deleting data is a key part of the CRUD operations in Health Connect. This guide
shows you how you can delete records in two ways.
Delete using Record IDs
You can delete records using a list of unique identifiers such as the Record ID
and your app's Client Record ID. Use deleteRecords
, and
supply it with two lists of Strings
, one for the Record IDs and one for the
Client IDs. If you only have one of the IDs available, you can set emptyList()
on the other list.
The following code example shows how to delete Steps data using its IDs:
suspend fun deleteStepsByUniqueIdentifier(
healthConnectClient: HealthConnectClient,
idList: List<String>
) {
try {
healthConnectClient.deleteRecords(
StepsRecord::class,
idList = idList,
clientRecordIdsList = emptyList()
)
} catch (e: Exception) {
// Run error handling here
}
}
Delete using a time range
You can also delete data using a time range as your filter.
Use deleteRecords
, and supply it with a
TimeRangeFilter
object that takes
a start and end timestamp values.
The following code example shows how to delete data of Steps data on a
specific time:
suspend fun deleteStepsByTimeRange(
healthConnectClient: HealthConnectClient,
startTime: Instant,
endTime: Instant
) {
try {
healthConnectClient.deleteRecords(
StepsRecord::class,
timeRangeFilter = TimeRangeFilter.between(startTime, endTime)
)
} catch (e: Exception) {
// Run error handling here
}
}
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2023-10-10 UTC.
[null,null,["Last updated 2023-10-10 UTC."],[],[],null,["# Delete data\n\nDeleting data is a key part of the CRUD operations in Health Connect. This guide\nshows you how you can delete records in two ways.\n| **Tip:** For further guidance on deleting data, take a look at the [Android Developer video for reading and writing data](https://www.youtube.com/watch?v=NAx7Gv_Hk7E&t=299) in Health Connect.\n\nDelete using Record IDs\n-----------------------\n\nYou can delete records using a list of unique identifiers such as the Record ID\nand your app's Client Record ID. Use [`deleteRecords`](/reference/kotlin/androidx/health/connect/client/HealthConnectClient#deleteRecords(kotlin.reflect.KClass,kotlin.collections.List,kotlin.collections.List)), and\nsupply it with two lists of `Strings`, one for the Record IDs and one for the\nClient IDs. If you only have one of the IDs available, you can set `emptyList()`\non the other list.\n\nThe following code example shows how to delete Steps data using its IDs: \n\n suspend fun deleteStepsByUniqueIdentifier(\n healthConnectClient: HealthConnectClient,\n idList: List\u003cString\u003e\n ) {\n try {\n healthConnectClient.deleteRecords(\n StepsRecord::class,\n idList = idList,\n clientRecordIdsList = emptyList()\n )\n } catch (e: Exception) {\n // Run error handling here\n }\n }\n\nDelete using a time range\n-------------------------\n\nYou can also delete data using a time range as your filter.\nUse [`deleteRecords`](/reference/kotlin/androidx/health/connect/client/HealthConnectClient#deleteRecords(kotlin.reflect.KClass,androidx.health.connect.client.time.TimeRangeFilter)), and supply it with a\n[`TimeRangeFilter`](/reference/kotlin/androidx/health/connect/client/time/TimeRangeFilter) object that takes\na start and end timestamp values.\n\nThe following code example shows how to delete data of Steps data on a\nspecific time: \n\n suspend fun deleteStepsByTimeRange(\n healthConnectClient: HealthConnectClient,\n startTime: Instant,\n endTime: Instant\n ) {\n try {\n healthConnectClient.deleteRecords(\n StepsRecord::class,\n timeRangeFilter = TimeRangeFilter.between(startTime, endTime)\n )\n } catch (e: Exception) {\n // Run error handling here\n }\n }"]]