delete confirmation in laravel

delete confirmation in laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing Delete Confirmations in Laravel Blade Templates

The code snippet you provided shows a standard list of cities where each row includes an "Edit" and a "Delete" button. While clicking the edit link navigates to a safe page, deleting a record is irreversible by default. If a user clicks the trash icon accidentally, your application will immediately execute the destruction logic without giving the user a second thought. Adding a confirmation modal prevents this accidental data loss.

You have two primary architectural approaches to handle this: using an intermediate redirect with a flash message, or implementing a JavaScript-based SweetAlert2 or native modal directly in the Blade template. The first approach is often safer for large datasets, while the second offers a smoother user experience by keeping the context intact. Below are the implementation details for both strategies.

Redirect-Based Confirmation

The most robust method involves preventing the immediate deletion request and instead redirecting the browser to the delete route with a specific query parameter indicating the intent. When the user arrives at that URL, you check the parameter in your controller or closure. If it exists, you display an error page asking for final confirmation.

In your Blade file, modify the link as follows:

<a class="btn btn-danger" href="{{ route('city-delete', $result->my_id) }}" onclick="return confirm('Are you sure you want to delete this city?')">
  <i class="fa fa-trash"></i>
</a>

However, relying solely on the browser's native confirm() dialog can feel clunky. A more seamless pattern is to create a dedicated route that handles the "soft" delete confirmation. You would change your controller logic to redirect to a specific URL like /cities/{id}/delete-confirm instead of executing the delete immediately.

In your controller, you might implement something like this:

public function confirmDelete($id)
{
    return view('cities.confirm-delete', compact('id'));
}

public function destroy($id)
{
    $city = City::findOrFail($id);
    
    // Double-check the intent token or parameter if you moved to a separate route
    // Or rely on the fact that this route is only hit after clicking "Yes"
    
    $city->delete();

    return redirect()->route('cities.index')->with('success', 'City deleted successfully.');
}

The confirm-delete view displays a final warning message with a red "Delete Forever" button. This ensures that the destructive action only happens when the user explicitly clicks through two layers of confirmation. The trade-off here is an extra HTTP request and a slight delay in seeing the result, which might be overkill for small applications but provides maximum safety against accidental clicks.

JavaScript Modal with SweetAlert2

For a more modern feel without leaving the page, you can intercept the click event on the delete link and open a custom modal using JavaScript. This approach keeps the user on the current list view until they make a decision.

First, ensure you have a library like SweetAlert2 loaded in your layout or via CDN:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/sweetalert2@11">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

Update your Blade loop to add an onclick handler that prevents the default link behavior and triggers a custom modal:

<a class="btn btn-danger" href="{{ route('city-delete', $result->my_id) }}" 
   onclick="confirmDelete(event, {{ $result->my_id }})">
  <i class="fa fa-trash"></i>
</a>

Define the confirmDelete function in your JavaScript block:

function confirmDelete(event, id) {
    event.preventDefault(); // Stop the link from navigating immediately
    
    Swal.fire({
        title: 'Are you sure?',
        text: "This city will be deleted permanently and cannot be recovered.",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#3085d6',
        confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
        if (result.isConfirmed) {
            window.location.href = "{{ route('city-delete', $result->my_id) }}";
        }
    });
}

This pattern effectively decouples the visual "click" from the actual network request. The user sees a polished modal instead of a browser alert. If they cancel, nothing happens and they stay on the list. If they confirm, the browser navigates to your city-delete route, which then processes the removal.

Security Considerations

Regardless of the method you choose, never trust client-side confirmation alone for security-critical operations. Browsers can be manipulated, and JavaScript can be disabled. Always validate permissions and check that the user is authorized to delete the specific resource on your server side. The route controller (city-delete) should contain the findOrFail check and permission logic before executing $city->delete().

If you are managing a team or maintaining larger applications, consider using Laravel's built-in policy system to enforce these rules at the model level rather than relying solely on UI confirmation dialogs.

Our network