← All posts

SaaS account deletion done properly: grace period, export and a real purge

A "Delete my account" button that flips deleted = 1 deletes nothing. Here's how we structure the request, the cancellation window, the actual purge in the database and file storage, and what you need to keep.

SaaS account deletion done properly: grace period, export and a real purge

Key points

  • Deletion happens in two steps: a request that can be cancelled, then an automatic purge on a fixed date.
  • Foreign keys define once and for all what goes with the account and what stays.
  • Files, your payment provider and your email tools hold customer data too.

In many SaaS products, account deletion is the last feature built and the least tested. In apps we take over, it often comes down to a deleted flag set to 1: the customer can’t see anything any more, but their projects, files and teammates’ email addresses sit in the database forever. That clashes with the right to erasure under GDPR, and it’s a liability in its own right, because data you no longer use can still leak. This is the setup we put in place.

A cancellable request, not an instant wipe

Deleting on the spot feels cleaner, but accidental or impulsive requests happen, and an admin removing their whole company’s workspace shouldn’t lose everything in one click. So we record the request and set a purge date, usually 30 days out. Pick a window that fits your product and state it clearly in your terms:

function requestDeletion(PDO $pdo, Billing $billing, int $accountId): void
{
    $pdo->prepare(
        'UPDATE accounts
            SET deletion_requested_at = NOW(),
                purge_after = NOW() + INTERVAL 30 DAY
          WHERE id = ?'
    )->execute([$accountId]);

    $billing->cancelSubscriptionNow($accountId);              // no more charges
    $pdo->prepare('DELETE FROM sessions WHERE account_id = ?')->execute([$accountId]);
    $pdo->prepare('UPDATE api_keys SET revoked_at = NOW() WHERE account_id = ?')
        ->execute([$accountId]);
}

During that window the account is locked: subscription cancelled, sessions closed, API keys revoked. The confirmation email includes a cancel link that simply resets both dates to NULL. It’s also the right moment to offer a full export (CSV or JSON) that customers can download without emailing support.

Let the database decide what disappears

A hand-written DELETE per table always misses the table someone adds six months later. We’d rather declare the behaviour in the schema:

ALTER TABLE accounts
  ADD COLUMN deletion_requested_at DATETIME NULL,
  ADD COLUMN purge_after DATETIME NULL;

-- Invoices outlive the account; everything else goes with it.
ALTER TABLE invoices MODIFY account_id BIGINT UNSIGNED NULL;
ALTER TABLE invoices ADD CONSTRAINT fk_invoices_account
  FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE SET NULL;
ALTER TABLE projects ADD CONSTRAINT fk_projects_account
  FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE;

Invoices are kept for accounting obligations, whose length depends on your jurisdiction, so check with your accountant. Everything else cascades. Every new table tied to an account has to pick CASCADE or SET NULL explicitly, and that question is now on our migration review checklist.

The purge, and everything outside the database

A scheduled job processes due accounts in small batches:

function purgeDueAccounts(PDO $pdo, Storage $storage): void
{
    $ids = $pdo->query(
        'SELECT id FROM accounts WHERE purge_after <= NOW() ORDER BY purge_after LIMIT 20'
    )->fetchAll(PDO::FETCH_COLUMN);

    foreach ($ids as $id) {
        $pdo->beginTransaction();
        $pdo->prepare('DELETE FROM accounts WHERE id = ?')->execute([$id]);
        $pdo->prepare('INSERT INTO purge_log (account_id, purged_at) VALUES (?, NOW())')
            ->execute([$id]);
        $pdo->commit();

        // After the commit: orphaned files can be retried, lost rows cannot.
        $storage->deletePrefix("accounts/{$id}/");
    }
}

Files are removed after the transaction commits. If storage fails, you retry the folder deletion; the other way round, you could end up with an intact account and no files.

  • Payment provider: delete or anonymise the customer record if their API allows it.
  • Email and CRM tools: remove the account’s contacts.
  • Backups: they still hold the data until they rotate out. Rather than editing them, document how long they’re kept and say so in your privacy policy.
  • Logs: keep personal data out of them in the first place, since they’re rarely purged.

FAQ

Should inactive accounts be deleted too?

Keeping abandoned accounts forever raises the same questions. A clear policy (an email reminder, then deletion after a defined period) reuses exactly the same purge-date mechanism.

What about a user leaving a team, not the whole account?

That’s a different case: you remove the user, but what they created belongs to the team’s account. Reassign their content rather than cascading it away.

Building a SaaS product and want customer data handled cleanly from day one? We design the architecture and the processes around it.

Share LinkedIn Facebook X WhatsApp
</>

webtestudio

Web agency: custom WordPress sites, premium themes and SaaS software.

Discuss your project →