刪除資料
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
將資料刪除是 Health Connect CRUD 作業中重要的一環。本指南
會說明刪除記錄的兩種方式。
使用記錄 ID 進行刪除
您可以使用專屬 ID 清單刪除記錄,例如記錄 ID
和應用程式的用戶端記錄 ID使用 deleteRecords
,以及
請提供兩份 Strings
清單,一份為記錄 ID 清單,另一份針對
用戶端 ID。如果只能使用其中一種 ID,則可設定 emptyList()
與另一個名單上的使用者
以下程式碼範例顯示如何使用 ID 刪除步數資料:
suspend fun deleteStepsByUniqueIdentifier(
healthConnectClient: HealthConnectClient,
idList: List<String>
) {
try {
healthConnectClient.deleteRecords(
StepsRecord::class,
idList = idList,
clientRecordIdsList = emptyList()
)
} catch (e: Exception) {
// Run error handling here
}
}
使用時間範圍進行刪除
您也可以使用時間範圍做為篩選條件來刪除資料。
使用 deleteRecords
,並以
TimeRangeFilter
物件,其中包含
開始和結束的時間戳記值
以下程式碼範例顯示如何刪除
特定時間:
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
}
}
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],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 }"]]