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

murilo's avatar
Level 10

laravel action on delete or upload product , delete images - DB transactions

hello , I know that in laravel it has DB transactions . I am in dough how to work in this case -

  • I will delete images from this product ( those images are in AWS ) and update the table of product gallery . after delete images from the gallery of the product I will update product . so I have this DB transaction -
try {
    DB::beginTransaction();
	//1) update gallery 
	// 2) delete images from gallery , from AWS
	// 3) update product

    DB::commit();
} catch (\Exception $e) {
    DB::rollBack();

    // Handle the exception
}

well, it will work . BUT if has any error after the process - 2) delete images from gallery , from AWS . the DB will rolledBack , BUT those images will be deleted .

How wold be the best way to work with this, in this case ? does have any method that I can delete images after the DB::commit(); ?

other option wold be making a table for deleted_images . and make a scheduler every day to delete those images from the table ( delete images ) .

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To handle the scenario where you need to ensure that images are only deleted from AWS if the database transaction is successful, you can use a combination of Laravel's event system and database transactions. Here's a step-by-step solution:

  1. Create an Event and Listener: Create an event that will be fired after the database transaction is committed. The listener for this event will handle the deletion of images from AWS.

  2. Fire the Event After Commit: Use the DB::afterCommit method to fire the event only if the transaction is successful.

  3. Handle the Event: In the event listener, perform the deletion of images from AWS.

Here is a detailed implementation:

Step 1: Create an Event and Listener

First, generate the event and listener using Artisan commands:

php artisan make:event ImagesDeleted
php artisan make:listener DeleteImagesFromAWS

Step 2: Define the Event

In app/Events/ImagesDeleted.php, define the event to accept the images to be deleted:

namespace App\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ImagesDeleted
{
    use Dispatchable, SerializesModels;

    public $images;

    public function __construct(array $images)
    {
        $this->images = $images;
    }
}

Step 3: Define the Listener

In app/Listeners/DeleteImagesFromAWS.php, handle the deletion of images from AWS:

namespace App\Listeners;

use App\Events\ImagesDeleted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Storage;

class DeleteImagesFromAWS
{
    public function handle(ImagesDeleted $event)
    {
        foreach ($event->images as $image) {
            Storage::disk('s3')->delete($image);
        }
    }
}

Step 4: Register the Event and Listener

In app/Providers/EventServiceProvider.php, register the event and listener:

protected $listen = [
    'App\Events\ImagesDeleted' => [
        'App\Listeners\DeleteImagesFromAWS',
    ],
];

Step 5: Use the Event in Your Transaction

In your controller or service where you handle the transaction, fire the event after the commit:

use App\Events\ImagesDeleted;
use Illuminate\Support\Facades\DB;

try {
    DB::beginTransaction();

    // 1) Update gallery
    // 2) Delete images from gallery (but keep track of them)
    $imagesToDelete = ['image1.jpg', 'image2.jpg']; // Example images

    // 3) Update product

    DB::commit();

    // Fire the event after commit
    DB::afterCommit(function () use ($imagesToDelete) {
        event(new ImagesDeleted($imagesToDelete));
    });
} catch (\Exception $e) {
    DB::rollBack();

    // Handle the exception
}

Explanation

  • Event and Listener: The ImagesDeleted event carries the list of images to be deleted. The DeleteImagesFromAWS listener handles the actual deletion from AWS.
  • DB::afterCommit: This ensures that the event is only fired if the transaction is successfully committed.
  • Exception Handling: If any part of the transaction fails, the images are not deleted from AWS because the event is not fired.

This approach ensures that your images are only deleted from AWS if the database transaction is successful, maintaining data consistency.

1 like

Please or to participate in this conversation.