Skip to main content

Do Before Delete

Last updated 2/04/2026

Overview​

The Do Before Delete event is a server-side event that executes just before a record is deleted from the database. It runs during the delete operation, giving you the opportunity to inspect, validate, or prevent the deletion before it occurs.

This event provides access to the

old
object on the
context
parameter, this is the existing record currently stored in the database that is about to be deleted. By using this object, you can control whether the record should be deleted and enforce rules before the database is modified.

How it Works​

When an existing record is deleted:

  1. The Do Before Delete event executes on the server.
  2. The
    old
    object contains the current values stored in the database.
  3. The record has not been removed from the database.
  4. You can inpsect the record's data to determine if deletion should be allowed.
  5. Returning an error will prevent the deletion from being committed.
  6. If no error is returned, the record will be deleted from the database.

Use Cases​

  • Prevent deletion of records
  • Enforce business rules before allowing deletion
  • Stop deletion if related records exist
  • Require certain conditions to be met before deletion
  • Log or audit deletion attempts

Example​

This function runs before deleting a customer record, checks if the customer has related orders, prevents deletion if any exist, and ensures that only customers without dependencies can be removed.

JavaScript
Prevent deletion if related records exist
function doBeforeDelete(five, context, result) {
// The 'old' object is the record currently in the DB
const oldRecord = context.old;

// Check if the customer has related orders
const query = `
SELECT COUNT(*) AS count
FROM Orders
WHERE CustomerID = ?
`;

const queryResult = five.executeQuery(
query,
0,
oldRecord.CustomerID
);

// If related orders exist, prevent deletion
if (queryResult.recordCount() > 0) {
return five.createError("This customer cannot be deleted because they have existing orders.");
}

return five.success();
}