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.
unitOfWork.bulkDelete( tableName: string, objectIds: string[] | number[] );
// delete multiple objects represented by instances of the
// Map interface in the "objects" array. 
// Each object in the array must have the "objectId" key with 
// a valid  value to the object in the database. Objects 
// must be stored in the "tableName" table.
unitOfWork.bulkDelete( tableName: string, objects: object[] );
// delete multiple objects identified by their objectId values. 
// Each object in the "objects" array must have a value for the 
// "objectId" key. Objects will be deleted in a table which 
// corresponds to the class the objects are instances of. 
// This means that, for exampple, if the objects are of class
// Person, they 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.
unitOfWork.bulkDelete( objects: object[] );
// Delete all objects that match the "whereClause" query
// in a table with the tableName name.
unitOfWork.bulkDelete(tableName: string, whereClause: string);
// 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 
unitOfWork.bulkDelete(opResult: OpResult);

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:

const unitOfWork = new Backendless.UnitOfWork();

// compose a collection of objectIds to delete
const objectsToDelete = [
 "5220681E-AB99-D53C-FF6C-C97ED0C65D00", 
 "C2937EA2-182A-49BC-FFDD-3A34B41CF200",
 "B4E120E5-1990-22B2-FF80-AD0AE9ACB100"
]

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

// run the transaction
unitOfWork.execute()
   .then(function (unitOfWorkResult) {
       // transaction is complete. Use uowResult to check the result
    })
   .catch( function( error ) {
       // Server reported an error.
   });
const unitOfWork = new Backendless.UnitOfWork();

// 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.
const ordersToDelete = [
{objectId: "D426CE1C-ACAF-B352-FF7D-4881EF280700"},
{objectId: "C2937EA2-182A-49BC-FFDD-3A34B41CF200"},
{objectId: "EB68A9E1-DC24-C961-FFC4-2EF9227BF700"}
]

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

// run the transaction
unitOfWork.execute()
   .then(function (unitOfWorkResult) {
       // transaction is complete. Use uowResult to check the result
    })
   .catch( function( error ) {
       // Server reported an error.
   });

const unitOfWork = new Backendless.UnitOfWork();

// 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.
const ordersToDelete = [
new Order({objectId: "EB68A9E1-DC24-C961-FFC4-2EF9227BF700"});
new Order({objectId: "F176163C-9F1D-BFEB-FF0C-99F311735400"});
new Order({objectId: "668BF86B-14F6-B6A9-FF5E-BE999C497900"});
]

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

// run the transaction
unitOfWork.execute()
   .then(function (unitOfWorkResult) {
       // transaction is complete. Use uowResult to check the result
    })
   .catch( function( error ) {
       // Server reported an error.
   });
Order class:
function Order(args) {
    args = args || {};
    this.objectId = args.objectId || "";
    this.orderStatus = args.orderStatus || ""
}

const unitOfWork = new Backendless.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()
   .then(function (unitOfWorkResult) {
       // transaction is complete. Use uowResult to check the result
    })
   .catch( function( error ) {
       // Server reported an error.
   });
const unitOfWork = new Backendless.UnitOfWork();

// compose a query. It will identify all Order
// objects where the "orderStatus" column has the
// value of "delivery pending".
const queryBuilder = Backendless.DataQueryBuilder.create();
queryBuilder.setWhereClause( "orderStatus = 'invalid'" );

const invalidOrders = unitOfWork.find( "Order", queryBuilder );

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

// run the transaction
unitOfWork.execute()
   .then(function (unitOfWorkResult) {
       // transaction is complete. Use uowResult to check the result
    })
   .catch( function( error ) {
       // Server reported an error.
   });