Skip to content

Delete Relation using condition

The API deletes objects from a relationship with their parent. The objects are identified implicitly through a whereClause condition.

Blocking API

int result = Backendless.Data.Of( "TABLE-NAME" ).DeleteRelation( 
                               Dictionary<string, object> parentObject,
                               string relationColumnName,
                               string whereClause );
int result = Backendless.Data.Of<E>().DeleteRelation( 
                              E parentObject,
                              string relationColumnName,
                              string whereClause );

Non-Blocking API

Backendless.Data.Of( "TABLE-NAME" ).DeleteRelation( 
                  Dictionary<string, object> parentObject,
                  string relationColumnName,
                  string whereClause,
                  AsyncCallback<int> callback );
Backendless.Data.Of<E>().DeleteRelation( 
                 E parentObject,
                 string relationColumnName,
                 string whereClause,
                 AsyncCallback;<int> callback );

where:

Argument                Description
TABLE-NAME Name of the table where the parent object is stored.
E .NET class of the parent object. The class name identifies the table where the parent object is stored.
parentObject The object for which the relation with the identified children will be deleted. When this argument is an instance of Dictionary<string, object> (for the dictionary-based approach), it must contain the "objectId" property.
relationColumnName Name of the column representing the relation. Relationship between the child objects identified by whereClause will be deleted for this column in parentObject.
whereClause A where clause condition identifying the objects in the child table which will be removed from the relation to the parent object.
callback A responder object which will receive a callback when the relation has been deleted or if an error occurs. Applies to the asynchronous method only.

Return Value

Number of child objects removed from the relationship. The asynchronous call receives the return value through a callback executed on the AsyncCallback object.

Example

The following example deletes a relation between a Person object and all the related objects in the related table identified by column "user" which match the provided query:

name='Joe' or name = 'Frank' 

As a result of the operation, all related objects where the name property is either Joe or Frank will be deleted from the relation.

Dictionary<string, object> parentObject = new Dictionary<string, object>();
parentObject[ "objectId" ] = "41230622-DC4D-204F-FF5A-F893A0324800";

AsyncCallback<int> deleteRelationCallback = new AsyncCallback<int>(
  howMany =>
  {
    System.Console.WriteLine( String.Format( "Server has removed {0} objects from the relation", howMany ) );
  },
  error =>
  {
    System.Console.WriteLine( "Server returned an error " + error.Message );
  } );

Backendless.Data.Of( "Person" ).DeleteRelation( parentObject, 
                  "user", 
                  "name = \"Joe\" or name = \"Frank\"", 
                  deleteRelationCallback );
Person personObject = // personObject retrieval is out of scope in this example

AsyncCallback<int> deleteRelationCallback = new AsyncCallback<int>(
  howMany =>
  {
    System.Console.WriteLine( String.Format( "Server has removed {0} objects from the relation", howMany ) );
  },
  error =>
  {
    System.Console.WriteLine( "Server returned an error " + error.Message );
  } );

Backendless.Data.Of<Person>().DeleteRelation( 
                  personObject, 
                  "user", 
                  "name = \"Joe\" or name = \"Frank\"",
                  deleteRelationCallback );

Codeless Reference

data_delete_object_where_clause

where:

Argument                Description
table name Name of the table where which contains the parent object as identified by parent object.
parent object Id of the object for which the relation will be deleted.
relation name Name of the column which identifies the relation within the parent table (identified as table name).
children You must use the where clause condition in this property to delete specific children objects from the data table.
return result When this box is checked, the operation returns the number of removed child objects relations.

Returns the number of removed child objects relations.

Consider the first object with one-to-many relations(skills column) in the parent data table called employees:
data_delete_relation_using_condition_1

By clicking the record (1:N Relations) in the skills column of the parent data table presented above, you get redirected to the child data table called uniqueSkills, where you can see the related children objects:

data_delete_relation_using_condition_2

Suppose, you want to delete only one relation. The example below deletes the Objective-C relation from the data table using the where clause condition "skill = 'Objective-C'" specified in the children property:

data_delete_relation_using_condition_3

The result of this operation is one deleted relation Objective-C from the data table:

data_delete_relation_using_condition_4

In case you want to delete multiple objects relations using the where clause condition, refer to the example below which removes all relations except the Java.

data_delete_relation_using_condition_5

After the Codeless Logic runs, only one object relation Java remains in the data table.

data_delete_relation_using_condition_6