Support and Documentation

Delete life cycle

As editors delete items in the UI, Brightspot deletes corresponding records in the database. You can override callbacks to provide additional functionality during the delete life cycle. For example, when Brightspot successfully deletes an item, you can write a message to a log file.

. See also:
Understanding the delete life cycle

The delete save life cycle starts with the Record#delete method. The following diagram shows the various callbacks you can use during this life cycle.

Delete life cycle
Figure 93. Delete life cycle


Referring to the previous diagram—

  • The available callbacks beforeDelete and afterDelete have empty implementations in Record, so you can override them in your own classes.

  • delete is not an available callback, and cannot be overridden.

Delete life cycle callbacks

This section describes the various callbacks during the delete life cycle. These callbacks correspond to the diagram Delete life cycle.

Note

If you need to save or delete an object in the following callbacks, always use saveImmediately or deleteImmediately. If you need to save or delete multiple objects, consider using a transaction (see Transactions) with Database#beginIsolatedWrites.

beforeDelete

Use beforeDelete to provide validation, referential integrity, or messaging before actually deleting an object. The following snippet deletes all of an author's articles from the database before deleting the author.

import com.psddev.cms.db.Content;
import com.psddev.dari.db.Query;

public class Author extends Content {

    /* Delete all of an author's articles before actually deleting the author. */
    @Override
    protected void beforeDelete() {
        Query.from(Article.class).where("author = ?", this.getId()).deleteAll();
    }
}
afterDelete

Use afterDelete to provide processing after the database delete, typically for sending messages to the UI or to log files. The following snippet overrides the afterDelete callback to write a message to stdout.

import com.psddev.cms.db.Content;

public class Author extends Content {

    @Override
    protected void afterDelete() {
        System.out.println("Deleted an author with ID " + this.getId());
    }
}
27-Feb-2020 15:41:38 INFO Deleted an author with ID 00000170-8861-d763-a7f7-fff91cee0000