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:^(NSInteger)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;

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

// Removes an existing object identified by its objectId from the Backendless database   
func removeById(objectId: String, responseHandler: ((Int) -> Void)!, errorHandler: ((Fault) -> Void)!)
// Removes an existing object from the Backendless database
- (void)removeWithEntity:(NSDictionary<NSString *,id> * _Nonnull)entity responseHandler:^(NSInteger)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;

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

// Removes an existing object identified by its objectId from the Backendless database
func removeById(objectId: String, responseHandler: ((Int) -> 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](https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html#//apple_ref/doc/uid/TP40014216-CH5-ID86)        .

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:^(NSInteger 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:^(NSInteger 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:^(NSInteger 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:^(NSInteger 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 ?? "")")
})

Codeless Reference

data_service_delete_object

where:

Argument                Description
table name Name of the data table where an object will be deleted from.
object or objectId This parameter expects either a unique identifier(objectId) of the record that must be deleted in the data table, or a data object which must contain the objectId property which identifies the object in the database. String value or number.

This operation does not return a value.

Consider the following records in the employees data table:

data_service_example_delete_object

The example below deletes the object associated with the objectId: "8948E66F-67DE-441B-A7F8-DAB89965E27C".

data_service_example_delete_object_2

The result of this operation will look as shown below after the Codeless logic runs. As you can see, the object with the name "Bob Smith" associated with the objectId: "8948E66F-67DE-441B-A7F8-DAB89965E27C" has been deleted from the data table.

data_service_example_delete_object_3

You can also delete a record from the data table by passing an object to the operation. The object must have the objectId property and the unique identifier of the record that must be deleted. The example below deletes a record from the data table using an object that contains the following objectId: "FEA94D74-BEA1-4F28-BDDD-FB22CFEB747E"

data_service_example_delete_object_4

The result of this operation is a deleted record from the employees data table:

data_service_example_delete_object_5