Back to Devexpress

Session.Delete(ICollection) Method

xpo-devexpress-dot-xpo-dot-session-dot-delete-x28-system-dot-collections-dot-icollection-x29.md

latest5.9 KB
Original Source

Session.Delete(ICollection) Method

Deletes persistent objects and their aggregated objects from the data store.

Namespace : DevExpress.Xpo

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

csharp
public void Delete(
    ICollection objects
)
vb
Public Sub Delete(
    objects As ICollection
)

Parameters

NameTypeDescription
objectsICollection

A collection of persistent objects to delete from the data store.

|

Remarks

If a persistent object has the DeferredDeletionAttribute, the Delete method does not physically delete the underlying database record. It uses a hidden GCRecord field to mark the object as deleted. Clear all references to the object to delete this object when the Session.PurgeDeletedObjects method is called.

If the DeferredDeletionAttribute is not set, the Delete method removes the underlying database record if this operation does not violate foreign key constraints.

How to Delete All Objects in an XPCollection

The following code deletes all objects in an XPCollection and deletes the corresponding database records:

csharp
using DevExpress.Xpo;  
using DevExpress.Data.Filtering;  
// ...  
var colDelete = new XPCollection<YourPersistentType>(theSession, new BinaryOperator("YourPropertyName", true)); // Objects for deletion.
theSession.Delete(colDelete);
vb
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
' ...
Dim colDelete = New XPCollection(Of YourPersistentType)(theSession, New BinaryOperator("YourPropertyName", True))
theSession.Delete(colDelete)

Note

When an object with Deferred Deletion enabled is deleted, it is removed from associated collections. Associations cannot be recreated automatically when you restore a deleted object. This behavior is by design, because the Deferred Deletion feature is not intended for object restoration, but for overcoming complex foreign index constraints at the database level.

The following code snippets (auto-collected from DevExpress Examples) contain references to the Delete(ICollection) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

XPO_how-to-implement-odata4-service-with-xpo-netcore/CS/ODataService/Helpers/DemoDataHelper.cs#L13

csharp
XPCollection<OrderDetail> orderDetails = new XPCollection<OrderDetail>(uow);
uow.Delete(orderDetails);
uow.Delete(docs);

XPO_how-to-implement-odata4-service-with-xpo/CS/ODataService/Helpers/DemoDataHelper.cs#L18

csharp
XPCollection<OrderDetail> orderDetails = new XPCollection<OrderDetail>(uow);
uow.Delete(orderDetails);
uow.Delete(docs);

XPO_how-to-generate-a-sequential-number-for-a-business-object-within-a-database-transaction-e2620/CS/ExplicitUnitOfWorkDemo/frmMain.cs#L115

csharp
XPCollection<Sequence> sequencList = new XPCollection<Sequence>(uow);
uow.Delete(personList);
uow.Delete(addressList);

XPO_how-to-create-web-service-supporting-the-data-level-caching-e2740/CS/DatabaseUpdater/Program.cs#L15

csharp
uow.CreateObjectTypeRecords(typeof(Person).Assembly);
uow.Delete(new XPCollection<Person>(uow));
uow.CommitChanges();

XPO_how-to-generate-a-sequential-number-for-a-business-object-within-a-database-transaction-e2620/VB/ExplicitUnitOfWorkDemo/frmMain.vb#L120

vb
Dim sequencList As New XPCollection(Of Sequence)(uow)
uow.Delete(personList)
uow.Delete(addressList)

See Also

Save

Deferred Object Deletion

How to delete a large set of persistent objects

How to create an empty XPCollection of a specified type

Session Class

Session Members

DevExpress.Xpo Namespace