Skip to content

Deleting Single Object

Blocking Method

Long result = Backendless.Data.Of( "TABLE-NAME" ).Remove( String objectId );
Long result = Backendless.Data.Of( "TABLE-NAME" ).Remove( Dictionary<string, object> entity );
public long Backendless.Data.Of<E>().Remove( String objectId );
public long Backendless.Data.Of<E>().Remove( E entity );

Non-Blocking API

void Backendless.Data.Of( "TABLE-NAME" ).Remove( String objectId, 
                                               AsyncCallback<long> responder )
void Backendless.Data.Of( "TABLE-NAME" ).Remove( Dictionary<string, object> entity, 
                                               AsyncCallback<long> responder )
public void Backendless.Data.Of<E>().Remove( String objectId, AsyncCallback<long> responder )
public void Backendless.Data.Of<E>().Remove( E entity, AsyncCallback<long> responder )

where:

Argument                Description
TABLE-NAME Name of the table where the object represented by System.Collections.Generic.Dictionary or objectId will be deleted.
E A .NET class of the data object to delete.
entity .NET object to delete, must be of type E or System.Collections.Generic.Dictionary (depending on the method used). Must contain a value for the objectId property which identifies the object to be deleted.
responder a responder object which will receive a callback when the method successfully deletes the object or if an error occurs. Applies  to the non-blocking method only.

Return Value

The blocking method returns the timestamp when the object is deleted. The non-blocking call receives the return value through a callback executed on the AsyncCallback object.

Example

Blocking API

``` csharp
public static void DeleteContact()
{
  // create new contact object first. Then we will delete it.
  Dictionary<String, Object> contact = new Dictionary<String, Object>();
  contact.Add( "name", "Jack Daniels" );
  contact.Add( "age", 147 );
  contact.Add( "phone", "777-777-777" );
  contact.Add( "title", "Favorites" );

  Dictionary<String, Object> savedContact = Backendless.Persistence.Of( "Contact" ).Save( contact );

  // now delete the saved object
  Backendless.Persistence.Of( "Contact" ).Remove( savedContact );
}
```

Non-blocking API

``` java
public static void DeleteContactAsync()
{
  // create new contact object first. Then we will delete it.
  Dictionary<String, Object> contact = new Dictionary<String, Object>();
  contact.Add( "name", "Jack Daniels" );
  contact.Add( "age", 147 );
  contact.Add( "phone", "777-777-777" );
  contact.Add( "title", "Favorites" );

  AsyncCallback<long> deleteObjectCallback = new AsyncCallback<long>(
    deletionTime =>
    {
      System.Console.WriteLine( "object has been deleted" );
    },
    error =>
    {
    }
  );

  AsyncCallback<Dictionary<String, Object>> saveObjectCallback = new AsyncCallback<Dictionary<String, Object>>(
    savedContact =>
    {
      System.Console.WriteLine( "object has been created" );
      // now delete the saved object
      Backendless.Persistence.Of( "Contact" ).Remove( savedContact, deleteObjectCallback );
    },
    error =>
    {
    }
  );

  Backendless.Persistence.Of( "Contact" ).Save( contact, saveObjectCallback );
}
```

Consider the following class:

package com.sample;

public class Contact
{
  private String objectId;
  private String name;
  private int age;
  private String phone;
  private String title;

  public String getObjectId() {
    return objectId;
  }

  public void setObjectId( String objectId ) {
    this.objectId = objectId;
  }

  public String getName() {
    return name;
  }

  public void setName( String name ) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge( int age ) {
    this.age = age;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone( String phone ) {
    this.phone = phone;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle( String title ) {
    this.title = title;
  }
}
The following code saves a new instance of the Contact class and subsequently updates it:

Blocking API

``` java
public void deleteContact()
{
    // make sure to put the initApp call somewhere early on 
    // in the app - main activity is the best place

    // save a new object first, so there is something to delete.
    Contact contact = new Contact();
    contact.setName( "Jack Daniels" );
    contact.setAge( 147 );
    contact.setPhone( "777-777-777" );
    contact.setTitle( "Favorites" );
    Contact savedContact = Backendless.Persistence.save( contact );

    // now delete the saved object
    Long result = Backendless.Persistence.of( Contact.class ).remove( savedContact );
 }
```

Non-Blocking API

``` java
public void deleteContact()
{
    // put the initApp call somewhere early on in your app, perhaps main activity

    // create a new object, so there is something to delete
    Contact contact = new Contact();
    contact.setName( "Jack Daniels" );
    contact.setAge( 147 );
    contact.setPhone( "777-777-777" );
    contact.setTitle( "Favorites" );

    Backendless.Persistence.save( contact, new AsyncCallback<Contact>() 
    {
      public void handleResponse( Contact savedContact )
      {
        Backendless.Persistence.of( Contact.class ).remove( savedContact, 
                                                            new AsyncCallback<Long>() 
        {
          public void handleResponse( Long response )
          {
            // Contact has been deleted. The response is the 
            // time in milliseconds when the object was deleted
          }
          public void handleFault( BackendlessFault fault )
          {
            // an error has occurred, the error code can be 
            // retrieved with fault.getCode()
          }
        } );
      }
      @Override
      public void handleFault( BackendlessFault fault )
      {
        // an error has occurred, the error code can be retrieved with fault.getCode()
      }
    });
 }
```

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