Instance Method


Deletes objects saved by this application that match the provided type and predicate.


func deleteObjects(of objectType: HKObjectType, predicate: NSPredicate, withCompletion completion: @escaping (Bool, Int, Error?) -> Void)



The type of object to be deleted.


A predicate used to filter the objects to be deleted. This method only deletes objects that match the predicate.


A block that this method calls as soon as the delete operation is complete. This block is passed the following parameters:


A Boolean value. This parameter contains true if the objects were successfully deleted; otherwise, false.


The number of objects deleted.


An error object. If an error occurred, this object contains information about the error; otherwise, it is set to nil.


Your app can delete only those objects that it has previously saved to the HealthKit store. If the user revokes sharing permission for an object type, you can no longer delete those objects. This method operates asynchronously. As soon as the delete operation is finished, it calls the completion block on a background queue.

If your app has not requested permission to share an object’s data type, the method fails with an HKError.Code.errorAuthorizationNotDetermined error. If your app has been denied permission to share an object’s data type, it fails with an HKError.Code.errorAuthorizationDenied error. When deleting multiple objects, if any object cannot be deleted, none of them are deleted.

HealthKit stores temporary HKDeletedObject entries, letting you query for recently deleted objects. However, the deleted objects are periodically removed to save storage space. If you want your app to receive notifications about all the deleted objects, set up an observer query, and enable it for background delivery. In the background query’s update handler, create an anchored object query to gather the list of recently deleted objects.

See Also

Working with HealthKit Objects

func delete(HKObject, withCompletion: (Bool, Error?) -> Void)

Deletes the specified object from the HealthKit store.

func delete([HKObject], withCompletion: (Bool, Error?) -> Void)

Deletes the specified objects from the HealthKit store.

func earliestPermittedSampleDate() -> Date

Returns the earliest date permitted for samples.

func save(HKObject, withCompletion: (Bool, Error?) -> Void)

Saves the provided object to the HealthKit store.

func save([HKObject], withCompletion: (Bool, Error?) -> Void)

Saves an array of objects to the HealthKit store.