Back to Devexpress

Delete Objects from the Database

expressappframework-403619-data-manipulation-and-business-logic-create-read-update-and-delete-data-delete-objects-from-database.md

latest6.4 KB
Original Source

Delete Objects from the Database

  • Mar 09, 2026
  • 3 minutes to read

Useful API

IObjectSpace.Delete methodMarks the specified persistent object and its aggregated objects as deleted from a persistent storage.IObjectSpace.IsObjectToDelete methodIndicates whether the specified object has been deleted but not committed in the transaction currently in progress.IObjectSpace.GetObjectsToDelete methodReturns a collection of persistent objects that will be deleted when the current transaction is committed, including objects that will be deleted in the parent transaction(s), optionally.IObjectSpace.CommitChanges methodSaves all the changes made to the persistent objects belonging to the current Object Space to the database.IObjectSpace.IsDeleting propertyIndicates whether the current Object Space is about to delete an object(s).IObjectSpace.CustomDeleteObjects eventThe IObjectSpace.Delete method raises the CustomDeleteObjects event. Handle this event to replace the default persistent object deletion logic with custom logic.IObjectSpace.ObjectDeleting eventOccurs when the specified objects are about to be deleted.IObjectSpace.ObjectDeleted eventOccurs after the specified objects have been deleted from the dataset.

In a Controller

  1. Create a new View Controller in a Module project (for example, in MySolution.Module).
  2. Use the ViewController.ObjectSpace property to access an Object Space for data-aware operations.
  3. Use the Object Space’s methods to delete an object.
  4. Call the CommitChanges() method to save changes.

The following Controller contains the Action that deletes completed tasks:

File : MySolution.Module\Controllers\TaskViewController.cs.

csharp
using DevExpress.ExpressApp;
using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base.General;
using System.Collections.Generic;
// ...
public class TaskViewController : ObjectViewController<ListView, Task> {
    public TaskViewController() {
        SimpleAction deleteCompletedTasksAction = 
            new SimpleAction(this, "Delete completed tasks", PredefinedCategory.Edit);
        deleteCompletedTasksAction.Execute += DeleteCompletedTasksAction_Execute;
    }
    private void DeleteCompletedTasksAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
        CriteriaOperator completedTasksCriteria = 
            CriteriaOperator.FromLambda<Task>(t => t.Status == TaskStatus.Completed);
        IList<Task> completedTasks = ObjectSpace.GetObjects<Task>(completedTasksCriteria);
        ObjectSpace.Delete(completedTasks);
        ObjectSpace.CommitChanges();
    }
}

Execute Custom Logic Instead of Deleting Objects

  1. Create a new View Controller in a Module project (for example, in MySolution.Module).
  2. In the overridden OnActivated method, subscribe to the CustomDeleteObjects event.
  3. In the event handler, implement custom logic and set the CustomDeleteObjectsEventArgs.Handled property to true to prevent execution of default logic.

The Controller below specifies the IsMarkedAsDeleted property of CustomDeleteObjectsEventArgs.Objects instead of removing them:

File : MySolution.Module\Controllers\CustomDeleteObjectController.cs.

csharp
using DevExpress.ExpressApp;
// ...
public class CustomDeleteObjectController : ObjectViewController<DetailView, Employee> {
    protected override void OnActivated() {
        base.OnActivated();
        ObjectSpace.CustomDeleteObjects += ObjectSpace_CustomDeleteObjects;
    }
    private void ObjectSpace_CustomDeleteObjects(object sender, CustomDeleteObjectsEventArgs e) {
        foreach (object deletingObject in e.Objects) {
            Employee employee = ObjectSpace.GetObject(deletingObject) as Employee;
            // IsMarkedAsDeleted is a custom Employee's property
            employee.IsMarkedAsDeleted = true;
        }
        e.Handled = true;
    }
    protected override void OnDeactivated() {
        ObjectSpace.CustomDeleteObjects -= ObjectSpace_CustomDeleteObjects;
        base.OnDeactivated();
    }
}

Note

This event is raised only when you use IObjectSpace methods to delete objects.

EF Core and XPO have similar functionality out of the box:

In an XPO Business Class

In XPO business class, you can perform additional business logic in the overridden XPBaseObject.OnDeleting method. The following code snippet demonstrates this. When you have a one-to-many association between two XPO business classes and delete an object of the one side of the association, the reference property of an object of the many side still contains the deleted object key. You can set this reference property value to null as demonstrated below.

csharp
using DevExpress.Persistent.BaseImpl;
// ...
public class Employee : Person {
    // ...
    protected override void OnDeleting() {
        base.OnDeleting();
        foreach (object obj in Session.CollectReferencingObjects(this)) {
            if (Session.GetClassInfo(obj).ClassType == typeof(DemoTask)) {
                ((DemoTask)obj).Notes += 
                string.Format("The assigned employee was removed: {0}.\r\n", FullName);
            }
        }
    }
}