Skip to main content
All docs

Delete Objects from the Database

  • 4 minutes to read

Useful API

IObjectSpace.Delete method
Marks the specified persistent object and its aggregated objects as deleted from a persistent storage.
IObjectSpace.IsObjectToDelete method
Indicates whether the specified object has been deleted but not committed in the transaction currently in progress.
IObjectSpace.GetObjectsToDelete method
Returns 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 method
Saves all the changes made to the persistent objects belonging to the current Object Space to the database.
IObjectSpace.IsDeleting property
Indicates whether the current Object Space is about to delete an object(s).
IObjectSpace.CustomDeleteObjects event
The IObjectSpace.Delete method raises the CustomDeleteObjects event. Handle this event to replace the default persistent object deletion logic with custom logic.
IObjectSpace.ObjectDeleting event
Occurs when the specified objects are about to be deleted.
IObjectSpace.ObjectDeleted event
Occurs 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(.vb).

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

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(.vb).

using DevExpress.ExpressApp;
// ...
public class CustomDeleteObjectController : ObjectViewController<DetailView, Employee> {
    protected override void 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;


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 (Nothing in VB) as demonstrated below.

using DevExpress.Persistent.BaseImpl;
// ...
public class Employee : Person {
    // ...
    protected override void 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);