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
// Dictionary interface in the "objectsToDelete" collection. 
// Each Dictionary 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<Dictionary<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 List<String>();
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
// add the BulkDelete operation to transaction
unitOfWork.BulkDelete( "Order", objectsToDelete.ToArray() );

// run the transaction
unitOfWork.Execute( new AsyncCallback<UnitOfWorkResult>(
callback =>
{
  //Transaction completed
},
fault =>
{
  //Server reported an error
} ) );
UnitOfWork unitOfWork = new UnitOfWork();

// create a collection of map objects
List<Dictionary<String, Object>> ordersToDelete = new List<Dictionary<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.
Dictionary<String, Object> order1 = new Dictionary<String, Object>();
order1[ "objectId" ] = "D426CE1C-ACAF-B352-FF7D-4881EF280700";
ordersToDelete.Add( order1 );

Dictionary<String, Object> order2 = new Dictionary<String, Object>();
order2[ "objectId" ] = "81556273-55A4-0C8C-FF85-6A32EC017B00";
ordersToDelete.Add( order2 );

Dictionary<String, Object> order3 = new Dictionary<String, Object>();
order3[ "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>(
callback =>
{
  //Transaction completed
},
fault =>
{
  //Server reported an error
} ) );

UnitOfWork unitOfWork = new UnitOfWork();

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

// 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.objectId = "1D0A453F-BE4E-ADE0-FFDB-8815368EB400";
ordersToDelete.Add( order1 );

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

Order order3 = new Order();
order3.objectId = "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>(
callback =>
{
  //Transaction completed
},
fault =>
{
  //Server reported an error
} ) );
Order class:
public class Order
{
   public String objectId;
   public String orderId;
   public String orderStatus;
}

// 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>(
callback =>
{
  //Transaction completed
},
fault =>
{
  //Server reported an error
} ) );
// 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 = uow.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>(
callback =>
{  
  //Transaction completed
},
fault =>
{
  //Server reported an error
} ) );