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 an object from the table with the name of tableName.
// The object must have a key/value pair for the "objectId" key.
unitOfWork.delete( tableName: string, object: object );
// 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 the type of classBasedObject is class Person, the object will be deleted from the 
// table Person. The class of classBasedObject must have a value for the objectId property.
unitOfWork.delete( classBasedObject );
// Delete an object identified by the objectId argument from the 
// table with the name of tableName
unitOfWork.delete( tableName: string, objectId: string );
// 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
unitOfWork.delete( opResult: OpResult );

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:

const unitOfWork = new Backendless.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
const order = {
  objectId: "EB68A9E1-DC24-C961-FFC4-2EF9227BF700"
}


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

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

// 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
const order = new Order({ 
  objectId: "EB68A9E1-DC24-C961-FFC4-2EF9227BF700"
});


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

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

const unitOfWork = new Backendless.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
const objectId = "EB68A9E1-DC24-C961-FFC4-2EF9227BF700";


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

// 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 );
const firstInvalidOrder = invalidOrders.resolveTo( 0 );

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

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