Deleting Data

    • class Table
    • Cake\ORM\Table::delete(Entity $entity, $options = [])
    • The will be applied. If the rulesfail, deletion will be prevented.
    • The Model.beforeDelete event is triggered. If this event is stopped, thedelete will be aborted and the event’s result will be returned.
    • The entity will be deleted.
    • All dependent associations will be deleted. If associations are being deletedas entities, additional events will be dispatched.
    • Any junction table records for BelongsToMany associations will be removed.
    • The Model.afterDelete event will be triggered.
      By default all deletes happen within a transaction. You can disable thetransaction with the atomic option:

    When deleting entities, associated data can also be deleted. If your HasOne andHasMany associations are configured as , delete operations will‘cascade’ to those entities as well. By default entities in associated tablesare removed using Cake\ORM\Table::deleteAll(). You can elect tohave the ORM load related entities, and delete them individually by setting thecascadeCallbacks option to true. A sample HasMany association with boththese options enabled would be:

    Note

    • There may be times when deleting rows one by one is not efficient or useful.In these cases it is more performant to use a bulk-delete to remove many rows atonce:

    A bulk-delete will be considered successful if 1 or more rows are deleted. Thefunction returns the number of deleted records as an integer.

    Warning

    New in version 3.4.1.