View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Maroš Kollár > DBIx-Class-DeleteAction-1.05 > DBIx::Class::DeleteAction



Annotate this POD


Open  0
View/Report Bugs
Module Version: 1.50.0   Source  


DBIx::Class::DeleteAction - Define delete triggers


 # Actor DBIC class
 package Your::Schema::Actor;
 use strict;
 use warnings;
 use base 'DBIx::Class';

 __PACKAGE__->add_columns(qw/id name/);
    'actorroles' => 'MyDB::Schema::ActorRole',
    { '' => '' },
    { delete_action => 'delete' }
 # Actor Role DBIC class
 package Your::Schema::ActorRole;
 use strict;
 use warnings;
 use base 'DBIx::Class';
 __PACKAGE__->add_columns(qw/id name actor production/);
    'actor' => 'MyDB::Schema::Actor',
    { '' => '' },
    { delete_action => 
        sub {
            my ($self,$params) = @_;
            # Do something special
    'production' => 'MyDB::Schema::Production',
    { '' => 'self.production' },
    { delete_action => 'deny' }
 # Somewhere else
 $schema->txn_do(sub {
 # Deletes all related actorroles only if they don't have a production
 # Finally deletes the actor itself (Always use transactions!!!)
 $schema->txn_do(sub {
 # Calls custom subroutine on actor
 # Denies deletion if a production is related


With this DBIx::Class component you can specify actions that should be triggered on a row delete. A delete action is specified by adding the 'delete_action' key to the optional attribute HASH reference when specifing a new relation (see DBIx::Class::Relationship).

The following delete actions are supported:

Custom delete handlers

If you set the delete_action to execute a method or a code reference the method will be called with the following parameters:



This method overdrives the DBIx::Class::Row delete method. You can add arbitrary data as HASHREF which will be passed to your custom delete handles.

Make sure that you ALWAYS call delete always inside a TRANSACTION block.

If you call another delete from within a custom delete handler always pass on the seen parameter.


Tree example

This example shows a very simple tree schema, where each node points to its parent node. Once you delete an item from the tree, all child nodes will be appended to the parent node of the deleted node.

 package MyApp::Treenode;
 use strict;
 use warnings;
 use parent qw(DBIx::Class);
 __PACKAGE__->add_columns(qw/id name parent/);
 # Do not delete parent node
    'parent' => 'MyApp::Treenode',
    { "" => "self.parent" },
    { delete_action => 'ignore' },
 # Update all child nodes
    'children' => 'MyApp::Treenode',
    { "foreign.parent" => "" },
    { delete_action => sub {
        my ($self,$params) = @_;
            parent  => $self->get_column('parent'), 
    } },


Use DBIC_TRACE=1 or set __PACKAGE__-storage->debug(1);> to see what is exactly going on.


Note that the delete method in DBIx::Class::ResultSet will not run DeleteAction triggers. See delete_all if you need triggers to run.

Any database-level cascade, restrict or trigger will be performed AFTER DBIx-Class-DeleteAction based triggers.

Always use transactions, or else you might end up with inconsistent data.


Please report any bugs or feature requests to, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your report as I make changes.


    Maroš Kollár
    maros [at]


This module was written for Revdev, a nice litte software company I run with Koki and Domm (


DBIx::Class::DeleteAction is Copyright (c) 2008-9 Maroš Kollár -

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.

syntax highlighting: