Skip to content

Deleting Single Object

Blocking API

Long result = Backendless.Persistence.of( "TABLE-NAME" ).remove( Map entity )
Long result = Backendless.Persistence.of( E ).remove( E entity );

Non-Blocking API

public void Backendless.Persistence.of( "TABLE-NAME" ).remove( Map entity, AsyncCallback<Long> responder )
Backendless.Persistence.of( E ).remove( E entity, AsyncCallback<Long> responder );

where:

Argument                Description
TABLE-NAME Name of the table where the object represented by java.util.Map will be deleted from. There must be objectId property in the map. The value of the property identifies the object which will be deleted.
E Java class of the data object to delete.
entity Java object to delete. Must contain the objectId property which identifies the object in the database.
responder a responder object which will receive a callback when the object is deleted or if an error occurs. Applies to the asynchronous method only.

Return Value

The blocking method returns the timestamp when the server-side removed the object from the data store. The non-blocking call receives the return value through a callback executed on the AsyncCallback object.

Example

Blocking API

public void deleteContact()
{
    // create new contact object first. Then we will delete it.
    HashMap contact = new HashMap();
    contact.put( "name", "Jack Daniels" );
    contact.put( "age", 147 );
    contact.put( "phone", "777-777-777" );
    contact.put( "title", "Favorites" );

    Map savedContact = Backendless.Persistence.of( "Contact" ).save( contact );

    // now delete the saved object
    Backendless.Persistence.of( "Contact" ).remove( savedContact );
 }

Non-Blocking API

public void deleteContact()
{
    // create new contact object first. Then we will delete it.
    HashMap contact = new HashMap();
    contact.put( "name", "Jack Daniels" );
    contact.put( "age", 147 );
    contact.put( "phone", "777-777-777" );
    contact.put( "title", "Favorites" );
    Backendless.Persistence.save( contact, new AsyncCallback<Map>() {
      public void handleResponse( Map savedContact )
      {
        // New contact object has been saved, now it can be deleted
        Backendless.Persistence.remove( savedContact, new AsyncCallback<Map>() {
          @Override
          public void handleResponse( Long response )
          {
            // Contact objectdhas been deleted
          }
          @Override
          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()
      }
    });
 }
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

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

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()
      }
    });
 }