I have a system with shared multitenancy, which means each table contains data for all tenants with a TenantId column to distinguish between them.
Provisioning a new tenant is quick and easy, however now I’m facing a challenge with deleting a single tenant.
Given that entities depend on each other for consistency, how do I delete a tenant easily from my database, while the system is in use by other tenants?
The system uses SQL Server 2008 R2, if that helps.
If I got you right – this is the classical case for use of
FOREIGN KEYSwithON CASCADEoption. You only delete one record from master tenants table and due to proper chain of FKeys the system deletes related records or updates the reference columns withNULLorDEFAULTvalueSometimes will not work in cases where table references itself with DELETE ON CASCADE