Skip to content

Deleting Single Object

Deleting an object in a data table must be done with a 'data store' object. A reference to a data store can be obtained using the following code:

// obtaining DataStore for the NSDictionary approach
MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"TABLE-NAME"];

// obtaining DataStore for the custom-class approach
DataStoreFactory *dataStore = [Backendless.shared.data of:[YOUR-CLASS class]];
// obtaining DataStore for the NSDictionary approach
let dataStore = Backendless.shared.data.ofTable("TABLE-NAME")

// obtaining DataStore for the custom-class approach
let dataStore = Backendless.shared.data.of(YOUR-CLASS.self)

where

Argument                Description
YOUR-CLASS identifies a table on the server.
"TABLE-NAME" is the name of the table for which to get the data store. All subsequent data store operations will be performed in the specified table.

Important

All Data Service methods in Backendless must be accessed through data stores.

A data store object provides the following APIs for deleting data objects in Backendless:

// Removes an existing object from the Backendless database
- (void)removeWithEntity:(id _Nonnull)entity responseHandler:^(NSNumber * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;

// Removes an existing object identified by its objectId from the Backendless database   
- (void)removeByIdWithObjectId:(NSString * _Nonnull)objectId responseHandler:^(NSNumber * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
// Removes an existing object from the Backendless database
func remove(entity: Any, responseHandler: ((NSNumber) -> Void)!, errorHandler: ((Fault) -> Void)!)

// Removes an existing object identified by its objectId from the Backendless database   
func removeById(objectId: String, responseHandler: ((NSNumber) -> Void)!, errorHandler: ((Fault) -> Void)!)

// Removes an existing object from the Backendless database
- (void)removeWithEntity:(NSDictionary<NSString *,id> * _Nonnull)entity responseHandler:^(NSNumber * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;

// Removes an existing object identified by its objectId from the Backendless database   
- (void)removeByIdWithObjectId:(NSString * _Nonnull)objectId responseHandler:^(NSNumber * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
// Removes an existing object from the Backendless database
func remove(entity: [String : Any], responseHandler: ((NSNumber) -> Void)!, errorHandler: ((Fault) -> Void)!)

// Removes an existing object identified by its objectId from the Backendless database
func removeById(objectId: String, responseHandler: ((NSNumber) -> Void)!, errorHandler: ((Fault) -> Void)!)

where:

Argument                Description
entity object to delete.
objectId ID of the object to delete.
responseHandler a block (closure) to handle successful result of an asynchronous call.
errorHandler a block (closure) to handle fault result of an asynchronous call.

Return Value

The API returns the number of removed objects from the database.

Example

Consider the following class which will be used in the example:
@interface Contact : NSObject

@property (strong, nonatomic) NSString *objectId;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *phone;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSNumber *age;

@end

@objcMembers class Contact: NSObject {
    var objectId: String?
    var name: String?
    var phone: String?
    var title: String?
    var age: NSNumber?
}

Important

SWIFT 4 note: The @objcMembers attribute is important. It is required since the Swift object properties are accessed from the Objective-C runtime. For additional information, see the Swift 4 programming guide.

The following code deletes an instance of the Contact class from the Backendless object storage:

Remove object from Backendless:
Contact *contact = // existing contact

DataStoreFactory *dataStore = [Backendless.shared.data of:[Contact class]];
[dataStore removeWithEntity:contact responseHandler:^(NSNumber *removed) {
    NSLog(@"Object has been removed");
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
Remove object identified by objectId from Backendless:
NSStrimg *contactId = // objectId of the existing contact

DataStoreFactory *dataStore = [Backendless.shared.data of:[Contact class]];
[dataStore removeByIdWithObjectId:contactId responseHandler:^(NSNumber *removed) {
    NSLog(@"Object has been removed");
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
Remove object from Backendless:
let contact: Contact = // existing contact

let dataStore = Backendless.shared.data.of(Contact.self)
dataStore.remove(entity: contact, responseHandler: { removed in
    print("Object has been removed")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})
Remove object identified by objectId from Backendless:
let contactId: String = // objectId of the existing contact

let dataStore = Backendless.shared.data.of(Contact.self)
dataStore.removeById(objectId: contactId, responseHandler: { removed in
    print("Object has been removed")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})
With the dictionary/map-driven approach, objects sent to the backend are represented as plain NSDictionary (in Objective-C) or [String: Any] Dictionary (in Swift):

Remove object from Backendless:
NSDictionary *contact = // existing contact

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Contact"];
[dataStore removeWithEntity:contact responseHandler:^(NSNumber *removed) {
    NSLog(@"Object has been removed");
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
Remove object identified by objectId from Backendless:
NSString *contactId = // objectId of the existing contact

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Contact"];
[dataStore removeByIdWithObjectId:contactId responseHandler:^(NSNumber *removed) {
    NSLog(@"Object has been removed");
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
Remove object from Backendless:
let contact: [String : Any] = // existing contact

let dataStore = Backendless.shared.data.ofTable("Contact")
dataStore.remove(entity: contact, responseHandler: { removed in
    print("Object has been removed")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})
Remove object identified by objectId from Backendless:
let contactId: String = // objectId of the existing contact

let dataStore = Backendless.shared.data.ofTable("Contact")
dataStore.removeById(objectId: contactId, responseHandler: { removed in
    print("Object has been removed")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})