When an entry is created with my plugin's field type, data is saved to a separate table with entries id for reference. I need to delete that data if the entry that is associated with the data is deleted. How and where should I listen for entry delete event to execute a method to delete this separate data?

2 Answers 2


You can add something like this to your plugin's init() method:

craft()->on('entries.onDeleteEntry', function(Event $event) {
    // Do your custom logic here.
    // Note that you can access the entry that was just deleted via $event->params['entry']

Or, better yet, from your custom table, make sure the foreign key that you've created on your entryId column back to the entries table has a CASCADE DELETE setup on it so that when Craft deletes the entry, it will cascade down and delete it from your table as well.

  • Is the CASCADE DELETE added to the defineRelations return array in the Record file?
    – a-am
    Commented Jul 8, 2014 at 4:36
  • 1
    Yeah... check out craft/app/records for examples: 'element' => array(static::BELONGS_TO, 'ElementRecord', 'id', 'required' => true, 'onDelete' => static::CASCADE),
    – Brad Bell
    Commented Jul 8, 2014 at 4:38
  • 1
    Or just see Lindsey's answer for an actual example. :)
    – Brad Bell
    Commented Jul 8, 2014 at 4:41
  • Sorry, I felt compelled to include a more elaborate example (and note my opinion on which method was best). But Brad's answer is definitely the right one, because it lists both ways of achieving this goal... including the event listener technique, which was originally mentioned by @aran.
    – Lindsey D
    Commented Jul 8, 2014 at 4:45
  • It's also worth noting that the event listener technique has some drawbacks... The example above is only listening for the deletion of an entry. And while that directly answers the original question, if your data were ever linked to an element other than an entry, that listener would be useless.
    – Lindsey D
    Commented Jul 8, 2014 at 4:47

Brad's answer is totally correct.

As the question was posed by the original asker, Brad provided an example of an event listener which would be triggered when an entry is deleted. He also mentions the CASCADE DELETE capabilities of a Record.

In my opinion, the absolute best way to achieve what you're doing is by properly setting up your Record. In your defineRelations method, you can create a proper relationship between your table and the main elements table...

class MyPlugin_MyRecord extends BaseRecord

    // ...

    public function defineRelations()
        return array(
            'element' => array(static::BELONGS_TO, 'ElementRecord', 'required' => true, 'onDelete' => static::CASCADE),

  • Also remember that a user might not want your data deleted when they delete the entry, so a database level cascade delete might not always be the best place to put the business rule. Using the event listener allows you to decide whether or not to delete the record. Commented Jul 8, 2014 at 6:42
  • For example what if the related record is a product that has orders against it? Deleting the entry might delete the product entry but you will still want to link to the historic product from order history. In that case I would pace a field on the record called 'deleted' as boolean. Anyways point is be careful with cascade delete logic on the db layer. Commented Jul 8, 2014 at 6:44
  • My plugin has two records both created when the entry my plugin's custom input type is associated with is saved. Both records use the entry's id as keys for their entries. Record (A) has a one to one relationship to the entry id. Record (B) has a one to many relationship with entry id. I am unclear with the information above how to setup the relation so 'onDelete' works.
    – a-am
    Commented Jul 8, 2014 at 16:55
  • It sounds like you'd just need to define that relationship in both of your records, nearly identical to the example code. Despite the fact that they are one-to-one VS one-to-many, I think they will both use BELONGS_TO because they are both the child tables of the main elements table. Check out this page on Yii relationship terms...
    – Lindsey D
    Commented Jul 8, 2014 at 17:14
  • I'm speculating a bit because I don't know exactly what your plugin does, but I'd say there's a good chance that you'd be better off relating your Records to elements, rather than directly to entries. Entries are a type of element (as are assets, users, etc). This would allow your field type to be associated with any element type, not just limited to entries. Disclaimer: I may be completely wrong here... You may have a valid reason for wanting to link your field type with entries exclusively.
    – Lindsey D
    Commented Jul 8, 2014 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.