4

I am using SoftDeletes for a model in Laravel5.

But in some cases (Keeping history is not useful),I want to do physical delete (Removing row from table)instead of softDelete.

    class PaymentInvoices extends Model {

    use SoftDeletes;
}

Is there any method for forcing physical delete?

gsk
  • 2,329
  • 8
  • 32
  • 56

1 Answers1

8

Of course there is. Use forceDelete method instead of just delete.

Keep in mind, forceDelete is only available if you use the SoftDeletes trait.

Example

$instance->delete() //This is a soft delete
$instance->forceDelete() // This is a 'hard' delete

More info here (scroll down to Permanently Deleting Models)

Harry Geo
  • 1,163
  • 3
  • 10
  • 24
  • 1
    `delete()` justs updates the `deleted_at` field to the deleted time. Thus that record is ignored in subsequent retrievals through Laravel (Eloquent API). But if you use raw select or other direct database access, surprise - it's there. – Alex P. Feb 25 '19 at 21:04
  • 1
    Of course it's there, Laravel just returns all the records that have `deleted_at` equal to `NULL`. There is no magic happening :) Check how the eloquent builder works [here](https://github.com/laravel/framework/blob/e6c8aa0e39d8f91068ad1c299546536e9f25ef63/src/Illuminate/Database/Eloquent/SoftDeletingScope.php) – Harry Geo Mar 16 '19 at 20:10
  • Thanks @Harry-Geo for the clarification and reference. I was speaking figuratively just to point out the pitfall of using mixed access including raw queries (not using Eloquent models) on the tables without filtering out records with non NULL `deleted_at` field value - which would return all records, including the soft deleted ones. – Alex P. Mar 21 '19 at 18:19