To delete associated samples we need to perform delete query on specific HKQuantityTypeIdentifier
.
To delete Active Energy from Workout refer below code:
let energyBurnedQuantity = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)
let predicate = HKQuery.predicateForObjects(from: workout)
let energyQuery = HKSampleQuery(sampleType: energyBurnedQuantity!, predicate: predicate, limit: 100, sortDescriptors: nil) { (query, result, error) in
if error == nil {
guard let resultData = result else {
return
}
if resultData.count > 0 {
self.healthStore?.delete(resultData, withCompletion: { [unowned self] (status, error) in
if status == true {
print("Successfully deleted Energy.")
})
} else {
print("Error \(String(describing: error?.localizedDescription))")
}
})
}
}
}
self.healthStore?.execute(energyQuery)