endTime defaults to startTime. Pass an endTime after startTime: on Android, a range that ends where it starts deletes nothing and returns false.
Types that map to the same platform type are deleted together: any sleep type deletes all of your app's sleep data in the range. On Android, either blood pressure type deletes whole blood pressure readings. On iOS, any headache type deletes all of your app's headache data.
Future<bool> deleteAllData(HealthDataType type) async { // Delete from a very old date to now return await health.delete( type: type, startTime: DateTime(2000, 1, 1), endTime: DateTime.now(), );}// Usageawait deleteAllData(HealthDataType.STEPS);
Pass the type of the data point, as in the quick reference. The signature makes it optional, but both platforms need it: without it, iOS throws an ArgumentError and Android throws a PlatformException.
// First, get the data with UUIDfinal dataPoints = await health.getHealthDataFromTypes( types: [HealthDataType.WEIGHT], startTime: DateTime.now().subtract(Duration(days: 7)), endTime: DateTime.now(),);// Delete a specific pointif (dataPoints.isNotEmpty) { final uuid = dataPoints.first.uuid; final success = await health.deleteByUUID(uuid: uuid, type: HealthDataType.WEIGHT); if (success) { print('Data point deleted'); }}
dataTypeKey is the type of the record. The optional recordId also deletes the record with that Health Connect record ID in the same call. On iOS, the method throws a MissingPluginException.
Generate a custom ID when you write the data, then delete by the same ID later. For a write method that accepts a client record ID, see the writeBloodPressure example in Writing data. Pass a clientRecordVersion with the clientRecordId: without a version, the plugin writes the record without the ID.
class RecordIdGenerator { static String generate(HealthDataType type, DateTime timestamp) { final dateStr = timestamp.toIso8601String(); return 'myapp_${type.name}_$dateStr'; }}// When writing data, use custom IDfinal recordId = RecordIdGenerator.generate( HealthDataType.WEIGHT, DateTime.now(),);// Later, delete by this IDawait health.deleteByClientRecordId( dataTypeKey: HealthDataType.WEIGHT, clientRecordId: recordId,);
Count the affected data points and ask the user to confirm before you delete. The count includes the data of other apps that your app can read, which delete does not remove:
Future<bool> safeDelete({ required Health health, required HealthDataType type, required DateTime startTime, required DateTime endTime, required Future<bool> Function(int count) confirmDeletion,}) async { // First, count the data points in the range final dataPoints = await health.getHealthDataFromTypes( types: [type], startTime: startTime, endTime: endTime, ); if (dataPoints.isEmpty) { print('No data to delete'); return false; } // Ask for confirmation final confirmed = await confirmDeletion(dataPoints.length); if (!confirmed) { print('Deletion cancelled'); return false; } // Proceed with deletion return await health.delete( type: type, startTime: startTime, endTime: endTime, );}// Usageawait safeDelete( health: health, type: HealthDataType.STEPS, startTime: startDate, endTime: endDate, confirmDeletion: (count) async { print('About to delete $count data points'); // In real app, show a dialog return true; // User confirmed },);
A delete returns false when it fails, for example because WRITE permission is missing or the platform does not have the type. It throws an ArgumentError when startTime is after endTime, and on Android an UnsupportedError when Health Connect is not available:
final types = [HealthDataType.STEPS, HealthDataType.WEIGHT];final granted = await health.requestAuthorization( types, permissions: List.filled(types.length, HealthDataAccess.WRITE),);if (granted) { // Can now delete data await health.delete( type: HealthDataType.STEPS, startTime: startTime, endTime: endTime, );}
On iOS, true from requestAuthorization means only that the request completed, not that WRITE permission was granted; check it with hasPermissions. On Android, the patterns that read the data first, such as Delete outliers, also need READ permission.