Delete database after deleting tenant
Describe the bug
As the docs wrote, if want to delete database, just get the database name prior to deleting the tenant using getDatabaseName()
Steps to reproduce
$tenant = tenancy()->find('303bc584-a142-46e4-9df7-f9cef10d92bb');
$tenant->getDatabaseName();
$tenant->delete();
Only tenant was deleted, database still alive.
Expected behavior
Database should be deleted
Your setup
- Laravel version: 7.6
- stancl/tenancy version: 2.3
- Storage driver: DB
Show me this part of your config. https://github.com/stancl/tenancy/blob/b51c7a399cc74a651dca2a16e3be0b1221d66b81/assets/config.php#L268-L278
/**
* Should tenant databases be deleted asynchronously in a queued job.
*/
'queue_database_deletion' => false,
/**
* Automatically delete the tenant's database after the tenant is deleted.
*
* This will save space but permanently delete data which you might want to keep.
*/
'delete_database_after_tenant_deletion' => false,
'delete_database_after_tenant_deletion' => false,
What are you surprised about?
If I set that option is true, it will delete the database event I don't run this: $tenant->getDatabaseName();
What do you mean?
oh its you !! HOW CAN I DELETE THE TENANT and it database
UPDATE your documentation
You can add the following event listener to your TenancyServiceProvider in order to auto delete the tenant's database:
Events\TenantDeleted::class => [
JobPipeline::make([
Jobs\DeleteDatabase::class,
])->send(function (Events\TenantDeleted $event) {
return $event->tenant;
})->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
],
Tested on:
- Laravel version: 11
stancl/tenancyversion: 3.8