Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shariff's avatar
Level 51

onDelete('cascade) not working

Hi

I Have 3 tables

1)policy (primary key)
2)project_declaration (foreign key for policy)
3)endorsement  (foreign key for policy)

when I delete a record in the policy table. The other records which are link to that table are not deleting. I am using onDelete('cascade) and I am using soft delete in all 3 tables.

my migration files


policies 

 Schema::create('policies', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('controll_number');
            $table->string('customer_name');
            $table->string('insured_name');
            $table->integer('policy_number');
            $table->string('policy_type');
            $table->date('start_date');
            $table->date('end_date');
            $table->string('insurance_company');
            $table->string('policy_sum_insured');
            $table->string('premium');
            $table->string('gst');
            $table->string('total_premium');
            $table->timestamps();
        });

project declaration

Schema::table('project_declerations', function (Blueprint $table) {

            $table->unsignedBigInteger('policy_number_id')->after('id')->nullable();
            $table->foreign('policy_number_id')->references('id')->on('policies')->onDelete('cascade');

            $table->unsignedBigInteger('Brand_id')->after('contact_person')->nullable();
            $table->foreign('Brand_id')->references('id')->on('brands')->onDelete('cascade');

            $table->date('intimation_date')->after('addredd')->default(now());

            
        });

endorsement

Schema::create('endorsements', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('legalentity');
            $table->unsignedBigInteger('policy_number');
            $table->string('location');
            $table->string('premium');
            $table->string('gst');
            $table->string('totalpremium');
            $table->date('start_date');
            $table->date('end_date');
            $table->string('suminsured');
            $table->text('description');
            $table->text('endorsement_filename');
            $table->foreign('legalentity')->references('id')->on('legal_entities')->onDelete('cascade');
            $table->foreign('policy_number')->references('id')->on('policies')->onDelete('cascade');
            $table->timestamps();
        });
0 likes
6 replies
Martal's avatar
Martal
Best Answer
Level 31

@matheenulla

onDelete('cascade) work on Sql level of your application and Sql level knows nothing about soft deletes. onDelete trigger at sql level will fire only on actual removing record from the table.

If you want to use soft deletes and make cascade deleting you have to subscribe on model deleting event

shariff's avatar
Level 51

@martal I am using MySQL and I want If I delete a parent record I want all child record should be deleted. How to do that one?

Martal's avatar

@matheenulla How many records will be deleted at the same time, what do you think?

Cascade soft deleting on php level of application will take some time so if there are lots records to delete your users will wait for a response for some time or - which is worse - processing of the request will overlap max execution time and your web server will kill a process.

If you have about 100 records to delete per time there is no problem to use events and listeners. If you have about 100000 records to delete - you should use queues.

shariff's avatar
Level 51

@martal ok thank bro for helping. I may delete not more than 10 records so I will go through the events and listeners. So I am seeing the documentation now. I will try that one now If I got some errors I will let you know thank you once again

Please or to participate in this conversation.