Skip to content

Deleting a single object

Important

This operation deletes a single object in the database. To delete more than one object, use the bulkDelete operation documented in the Deleting multiple objects chapter of this guide.

To add this operation to a Unit Of Work instance, use the following methods. The methods are organized by how the object to be deleted is identified:

// Delete the object represented as a Map from the table with the name of tableName.
// The mapBasedObject collection must have a key/value pair for the "objectId" key.
unitOfWorkInstance.delete( String tableName, Map<String, Object> mapBasedObject );
// Delete the object represented as an instance of a custom class. 
// Backendless deletes the object from a table with the same name 
// as the name of the class. For example, if YourCustomClass is a
// class called Person, the the object will be deleted from the 
// table Person. YourCustomClass must have public get/set method 
// for the objectId property or a public objectId field.
unitOfWorkInstance.delete( YourCustomClass classBasedObject );
// Delete an object identified by the objectId argument from the 
// table with the name of tableName
unitOfWorkInstance.delete( String tableName, String objectId );
// Delete an object which is a result of a prior operation in the 
// same transaction. This operation expects a single object, which 
// means the "result" argument can be the result from any of the 
// following operations:
//  - saving a single object
//  - updating a single object
unitOfWorkInstance.delete( OpResult result );

// Delete an object represented by a result from another operation 
// in the same transaction. The difference between this method and 
// the one above is the OpResultValueReference object represents 
// a value within an OpResult object. For example, an OpResult may
// be a collection of objects from the "Retrieving objects" operation.
// A single object from that collection is represented by a 
// OpResultValueReference instance used in this method. The 
// "valueReference" argument can be an object from a result from any
// of the following operations:
//  - retrieving objects
//  - saving multiple objects
unitOfWorkInstance.delete( OpResultValueReference valueReference );

Return Value

All methods return an instance of the OpResult class which represents the timestamp when the object was deleted from the database. The OpResult object can be used as an argument in other operations in the same transaction (same UnitOfWork instance). For more information see the Operation Result chapter of this guide.

Example

The examples below demonstrate the usage of the operation for all possible ways to reference the object that should be deleted:

// create unit of work - it represents the transaction
UnitOfWork unitOfWork = new UnitOfWork();

// create a map-based object.
// the only required property is objectId.
// in your application the object may come from
// an API used outside of the transaction, or you
// can compose it the same way as shown below
Map<String, Object> orderObject = new HashMap<>();
orderObject.put( "objectId", "EB68A9E1-DC24-C961-FFC4-2EF9227BF700" );

// add delete operation to the transaction
unitOfWork.delete( "Order", orderObject );

// run the transaction
unitOfWork.execute( new AsyncCallback<UnitOfWorkResult>()
{
  @Override
  public void handleResponse( UnitOfWorkResult result )
  {
    Log.i( "MYAPP", "transaction complete - " + result );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", "Server reported an error " + fault );
  }
} );

// create unit of work - it represents the transaction
UnitOfWork unitOfWork = new UnitOfWork();

// create a class-based object.
// the only required property is objectId.
// in your application the object may come from
// an API used outside of the transaction, or you
// can compose it the same way as shown below
Order order = new Order();
order.setObjectId( "996E2034-4CC6-0BC4-FF01-E92077819700" );

// add delete operation to the transaction
unitOfWork.delete( order );

// run the transaction
unitOfWork.execute( new AsyncCallback<UnitOfWorkResult>()
{
  @Override
  public void handleResponse( UnitOfWorkResult result )
  {
    Log.i( "MYAPP", "transaction complete - " + result );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPPP", "Server reported an error " + fault );
  }
} );
The Order class is shown below:
package com.company;

import java.util.Date;

public class Order
{
  private String objectId;
  private Date deliveryDate;
  private String orderStatus;

  public String getObjectId()
  {
    return objectId;
  }

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

  public Date getDeliveryDate()
  {
    return deliveryDate;
  }

  public void setDeliveryDate( Date deliveryDate )
  {
    this.deliveryDate = deliveryDate;
  }

  public String getOrderStatus()
  {
    return orderStatus;
  }

  public void setOrderStatus( String orderStatus )
  {
    this.orderStatus = orderStatus;
  }
}

// create unit of work - it represents the transaction
UnitOfWork unitOfWork = new UnitOfWork();

// objectId of the object to delete.
String objectId = "37ADFFA8-39E2-73A5-FF44-651EF5C4E900";

// add delete operation to the transaction
unitOfWork.delete( "Order", objectId );

// run the transaction
unitOfWork.execute( new AsyncCallback<UnitOfWorkResult>()
{
  @Override
  public void handleResponse( UnitOfWorkResult result )
  {
    Log.i( "MYAPP", "transaction complete - " + result );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", "Server reported an error " + fault );
  }
} );
// create unit of work - it represents the transaction
UnitOfWork unitOfWork = new UnitOfWork();

DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "orderStatus = 'invalid'" );
OpResult invalidOrders = unitOfWork.find( "Order", queryBuilder );
OpResultValueReference firstInvalidOrder = invalidOrders.resolveTo( 0 );

// add delete operation to the transaction
unitOfWork.delete( firstInvalidOrder );

// run the transaction
unitOfWork.execute( new AsyncCallback<UnitOfWorkResult>()
{
  @Override
  public void handleResponse( UnitOfWorkResult result )
  {
    Log.i( "MYAPPP", "transaction complete - " + result );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", "Server reported an error " + fault );
  }
} );