tenancy icon indicating copy to clipboard operation
tenancy copied to clipboard

Delete database after deleting tenant

Open phuclh opened this issue 6 years ago • 7 comments

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

phuclh avatar Apr 29 '20 20:04 phuclh

Show me this part of your config. https://github.com/stancl/tenancy/blob/b51c7a399cc74a651dca2a16e3be0b1221d66b81/assets/config.php#L268-L278

stancl avatar Apr 29 '20 20:04 stancl

/**
     * 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,

phuclh avatar Apr 29 '20 22:04 phuclh

'delete_database_after_tenant_deletion' => false,

What are you surprised about?

stancl avatar Apr 29 '20 22:04 stancl

If I set that option is true, it will delete the database event I don't run this: $tenant->getDatabaseName();

phuclh avatar Apr 30 '20 01:04 phuclh

What do you mean?

stancl avatar Apr 30 '20 10:04 stancl

oh its you !! HOW CAN I DELETE THE TENANT and it database

UPDATE your documentation

Pblak avatar Jan 21 '23 21:01 Pblak

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/tenancy version: 3.8

abdelazizmirasoft avatar Aug 01 '24 20:08 abdelazizmirasoft