HealthKit (delete function in SwiftUI)

Hello

I am trying to save some data in the Health App from my app, and it is working, the problem is that when I delete that data (already saved) from my app (using the deleteFromHealthKit function) the data is not deleted from the health app. How can I fix this?

Here is the code:

import SwiftUI
import HealthKit


struct ContentView: View {
    
    
    init() {
        //--------
        let healthStore = HKHealthStore()
        let allTypes = Set([HKObjectType.quantityType(forIdentifier: .dietaryWater)!])
        
        healthStore.requestAuthorization(toShare: allTypes, read: allTypes) { (success, error) in
            if !success {
                print("success")
            }
        }
    }
    
    
    func fetchHealthData(date: Date, ml: Double) -> Void {
        let healthStore = HKHealthStore()
        let quantityType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryWater)
        let waterConsumed = HKQuantitySample.init(type: quantityType!, quantity: .init(unit: HKUnit.literUnit(with: .milli), doubleValue: ml), start: date, end: date)
        
        healthStore.save(waterConsumed) { success, error in
            if (error != nil) {
                print("Error: \(String(describing: error))")
            }
            if success {
                print("Saved: \(success)")
            }
        }
        
    }

    @State var water: [Water] = []
    @State private var value: Double = 0
    
    func deleteFromHealthKit(date: Date, ml: Double) {
        let healthStore = HKHealthStore()
        let quantityType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.dietaryWater)
        let waterConsumed = HKQuantitySample.init(type: quantityType!, quantity: .init(unit: HKUnit.literUnit(with: .milli), doubleValue: ml), start: date, end: date)
        
        healthStore.delete(waterConsumed) { success, error in
            if (error != nil) {
                print("Error: \(String(describing: error))")
            }
            if success {
                print("Saved: \(success)")
            }
            
        }
    }
    
    var body: some View {
        NavigationView{
            VStack{
                Text("Value: \(value)")
                    .padding()
                HStack{
                    Text("100 ml")
                        .onTapGesture {
                            value = 100
                        }
                    Text("200 ml")
                        .onTapGesture {
                            value = 200
                        }
                }
                Button("Add"){
                    water.append(Water(value: value, date: Date()))
                    fetchHealthData(date: Date(), ml: value)
                }.disabled(value == 0 ? true : false)
                .padding()
                
                List{
                    ForEach(0..<water.count, id: \.self){ i in
                        HStack{
                            Text("\(water[i].value)")
                    
                            Text("\(water[i].date)")
                        }
                        .onTapGesture {
                            deleteFromHealthKit(date: water[i].date, ml: water[i].value)
                            water.remove(at: i)
                        }
                    }
                }
            }
        }
    }
}

struct Water: Identifiable {
    var id = UUID()
    var value: Double
    var date: Date
}

Thank you

You need to get the uuid of the sample in the health database and delete that sample by its uuid. See delete(_:withCompletion:)

From the documentation of HKHealthStore.delete

object
    An object that this app has previously saved to the HealthKit store.

You can't delete an object that you just created as you do in your deleteFromHealthKit method. You need to query the objects saved in HealthKit, and then delete the objects returned. Or delete the object that you previously saved.

HealthKit (delete function in SwiftUI)
 
 
Q