Skip to content

Deleting multiple objects

This operation deletes multiple objects from the database. To add this operation to a Unit Of Work instance, use the following methods. The methods are organized by how the objects to be deleted are identified:

// delete multiple objects identified by using their objectId values 
// in the objectIdValues array. All referenced objects in the objectIdValues
// argument must be stored in a table with name of tableName.
unitOfWorkInstance.bulkDelete( String tableName, String[] objectIdValues );
// delete multiple objects represented by instances of the
// Map interface in the "objectsToDelete" collection. 
// Each Map object must have the "objectId" key with a valid 
// value to the object in the database. Objects must be stored
// in the "tableName" table.
unitOfWorkInstance.bulkDelete( String tableName, 
                               List<Map<String, Object>> objectsToDelete );
// delete multiple objects identified by their objectId values. 
// Each object is represented by an instance of YourCustomClass.
// The class must have a public getter/setter for the objectId 
// property or the public String objectId field with a valid value.
// Objects will be deleted in a table which corresponds to the 
// YourCustomClass class. This means that if YourCustomClass is
// called Person, the objects will be deleted from the Person 
// table. If a referenced object is not stored in the table
// the operation returns an error and the entire transaction 
// rolls back.
unitOfWorkInstance.bulkDelete( List<YourCustomClass> objects );
// Delete all objects that match the "whereClause" query
// in a table with the tableName name.
unitOfWorkInstance.bulkDelete( String tableName, String whereClause );
// Delete all objects identified by the "result" object. This must
// be a result from another operation in the same transaction. The 
// "result" object must represent a collection of objects to 
// delete. It can be a result of any of the following operations:
//   - retrieving objects
//   - saving multiple objects 
unitOfWorkInstance.bulkDelete( OpResult result );

Return Value

All methods listed above return an instance of the OpResult class which represents the number of objects deleted in the database. The returned 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:

UnitOfWork unitOfWork = new UnitOfWork();

// compose a collection of objectId's to delete
List<String> objectsToDelete = new ArrayList<>();
objectsToDelete.add( "5220681E-AB99-D53C-FF6C-C97ED0C65D00" );
objectsToDelete.add( "C2937EA2-182A-49BC-FFDD-3A34B41CF200" );
objectsToDelete.add( "B4E120E5-1990-22B2-FF80-AD0AE9ACB100" );

// convert the list to an array of string objects
String[] objectsToDeleteArray = new String[ objectsToDelete.size() ];
objectsToDeleteArray = objectsToDelete.toArray( objectsToDeleteArray );

// add the bulkDelete operation to transaction
unitOfWork.bulkDelete( "Order", objectsToDeleteArray );

// run the transaction
unitOfWork.execute( new AsyncCallback<UnitOfWorkResult>()
{
  @Override
  public void handleResponse( UnitOfWorkResult result )
  {
    System.out.println( "transaction complete - " + result );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    System.out.println( "Server reported an error " + fault );
  }
} );
UnitOfWork unitOfWork = new UnitOfWork();

// create a collection of map objects
List<Map<String, Object>> ordersToDelete = new ArrayList<Map<String, Object>>();

// here we construct map objects and populate them with
// the objectId values. It is the only required property
// for the deletion operation. Instead of creating these
// objects, they could be coming from the server from
// another API call.
Map<String, Object> order1 = new HashMap<>();
order1.put( "objectId", "D426CE1C-ACAF-B352-FF7D-4881EF280700" );
ordersToDelete.add( order1 );

Map<String, Object> order2 = new HashMap<>();
order2.put( "objectId", "81556273-55A4-0C8C-FF85-6A32EC017B00" );
ordersToDelete.add( order2 );

Map<String, Object> order3 = new HashMap<>();
order3.put( "objectId", "0442109E-15A8-3609-FFFE-D53ECF2A6300" );
ordersToDelete.add( order3 );

// add the bulkDelete operation to the transaction
unitOfWork.bulkDelete( "Order", ordersToDelete );

// 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 );
  }
} );
UnitOfWork unitOfWork = new UnitOfWork();

// create a collection of Order objects
List<Order> ordersToDelete = new ArrayList<>();

// here we construct Order objects and populate them with
// the objectId values. It is the only required property
// for the deletion operation. Instead of creating these
// objects, they could be coming from the server from
// another API call.
Order order1 = new Order();
order1.setObjectId( "1D0A453F-BE4E-ADE0-FFDB-8815368EB400" );
ordersToDelete.add( order1 );

Order order2 = new Order();
order2.setObjectId( "F176163C-9F1D-BFEB-FF0C-99F311735400" );
ordersToDelete.add( order2 );

Order order3 = new Order();
order3.setObjectId( "668BF86B-14F6-B6A9-FF5E-BE999C497900" );
ordersToDelete.add( order3 );

// add the bulkDelete operation to the transaction
unitOfWork.bulkDelete( ordersToDelete );

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

// add delete operation to the transaction.
// The first argument is the name of the table - "Order"
// The second argument is a query used to identify the
// order which should be deleted. Any order that has
// the orderStatus set to "invalid", will be deleted
// by this operation
unitOfWork.bulkDelete( "Order", "orderStatus = 'invalid'"  );

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

// compose a query. It will fetch all objects which
// have orderStatus set to "invalid".
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( "orderStatus = 'invalid'" );

// add the find operation to the transaction.
// Notice the result is stored in the "invalidOrder" variable
OpResult invalidOrders = unitOfWork.find( "Order", queryBuilder );

// add delete operation to the transaction.
// Notice the argument is the result from the previous operation
// in the transaction
unitOfWork.bulkDelete( invalidOrders );

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