Delete data

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
    }
}