Cascade Delete

If a repository item property references other items and the property’s cascade attribute is set to delete, removal of the repository item triggers removal of the referenced items. Also, when you remove a reference to this item, the item is automatically removed.

You should exercise caution when using cascading deletion in one-to-many relationships. Specifically, never set cascade to delete in properties on the “many” side of the relationship where those properties refer to items on the “one” side of the relationship. The item on the “one” side of the relationship cannot be deleted safely, because multiple items may be referring to it.

For example, an item descriptor company has an employee property that references many repository items defined by an employee item descriptor. The employee item descriptor itself defines a company property. In this one-to-many relationship, the employee property in the company item descriptor can set cascade to delete. However, the company property in the employee item descriptor must not set its own cascade attribute to delete; otherwise, the removal of one employee item would also entail removal of the company item that references all other employee items.

Removing null references

It sometimes happens that an item property references other items and one of those items is removed without explicitly removing the reference to it. This can occur when the database has no references constraint on the pertinent columns, so the referenced item can be removed without updating the referencing item’s property. It can also occur in a case where the referenced item might actually exist, but is currently filtered out by an RQL filter so it appears not to exist.

A repository item ignores references to missing items rather than returning an error if the referencing property sets its removeNullValues attribute to true. In this case, the missing item is returned as null to a scalar reference, and is omitted from the items that are returned for multi-valued references.

For example, a user profile might have a multi-valued property that is a list of favorite articles. Any given article might be deleted or become out of date. You can remove references to articles that are no longer available with the removeNullValues attribute like this:

Order of deletion

Depending on how the database schema defines reference constraints, you might need to control whether a cascading deletion occurs before or after deletion of the referencing item. You can specify the desired behavior in an item descriptor by setting the cascadeDeleteOrder attribute: