Skip to content

Delete Records Developer Guide

Ed Mozley edited this page Dec 5, 2026 · 2 revisions

Delete records - Developer Guide

(After 5.19.0, #530.) User side: Delete records. Design and decisions: Delete-Records-Design. Part 2 - the snapshot and Restore line by line: Delete records - Developer Guide 2.

The one idea: one tool with its own bin, not a soft-delete flag on every table (Ed, 5 Dec 2026 - weeks of work, and one missed WHERE deleted = 0 shows deleted data). A delete really deletes; what makes it safe is a snapshot of every row it touches, kept in record_bin and put back by Restore.

πŸ“ Files

File What it does
βš™οΈ includes/record_delete.php Everything: the plan, preview, retire, delete, bin list, restore, purge, log
πŸ”Œ api/system/delete_records.php GET find / preview / bin, POST retire / delete / restore / settings. admin_api_guard.php - administrators only. Purges the bin on every call
πŸ–₯️ system/delete-records/index.php, assets/js/delete-records.js The page: Delete and Bin tabs
πŸ—„οΈ record_bin (database/freeitsm.sql, includes/db_verify_schema.php, indexes generated) One row per delete; snapshot is NULL once restored or purged
🧭 system/maintenance/includes/tools.php, system/includes/areas.php The Maintenance card and its icon
❓ system/help/delete-records.php, _registry.php In-app help
🌐 lang/en/system.php (delete_records.*, landing.delete_records_*) Strings
πŸ§ͺ includes/feature_bingo/cards/system-core.php system.delete_records
πŸ‘€ includes/analyst_lifecycle.php record_bin.deleted_by_id, restored_by_id as history
βœ… tests/record-delete.php (32) See Developer-Tests-System

1. The plan - where it comes from

recordDeletePlan($conn, $type, $id) returns every column that names the record and has rows, with what a delete does there: delete (the row goes) or null (the row stays and lets go). It is never a second hand-kept list:

  • asset - AssetMergeService::REFERENCES, the list a merge moves. tests/asset-merge.php fails when a schema column that looks like an asset reference is missing from it, so a new table is caught there and reaches this tool too. Plus the POLYMORPHIC ones: document_links (parent_type = 'asset'), analyst_recent_trail (entity_type = 'asset'), and other assets' reference fields (asset_fields.field_type = 'ref' whose config.ref_kind is asset).
  • person - USER_DELETE_CASCADE (theirs: delete), USER_DELETE_SET_NULL (outlives them: null), tickets.user_id (null - the ticket keeps its subject), then everything userReferences() finds: every FK to users and every user_id / *_user_id column. USER_DELETE_LOGS are left alone. intune_devices.user_id is never touched - userReferences() skips it because it holds Entra's object id, not users.id.

For a column no list names, the action is what database/freeitsm.sql declares - read from the file, because a verified install has no foreign keys to ask:

function recordDeleteAction(PDO $conn, string $target, string $table, string $col): string
{
    $rule = recordDeleteSchemaRules($target)["$table.$col"] ?? null;
    if ($rule === 'CASCADE') return 'delete';
    if ($rule === 'SET NULL') return 'null';
    return recordDeleteNullable($conn, $table, $col) ? 'null' : 'delete';
}

With no rule declared, a column that may be empty is cleared and one that may not is deleted - a NOT NULL column cannot be cleared, and its row cannot exist without the record.

Assignments break that fallback: users_assets.user_id and software_licence_assignments.user_id / .asset_id may be empty (the same row can name an analyst, or the other of person/asset instead), so "empty means clear" would leave an asset held by nobody, a seat given to nobody. They are named in RECORD_DELETE_ASSIGNMENTS and go with the record, as unassigning would.

⚠️ TRAP: a new table whose row is a record's assignment, with a nullable column, belongs in RECORD_DELETE_ASSIGNMENTS. Read the "Kept, no longer naming it" rows of a preview after adding one.

⚠️ TRAP: nothing here may lean on the database to cascade. A verified install has no foreign keys, so a row not in the plan is left pointing at a record that no longer exists.

1b. Companies - only when empty (stage 3)

Ed's choice (5 December 2026, design): a company's plan is only its own settings - every column database/freeitsm.sql declares ON DELETE CASCADE to tenants (26 tables: tenant_domains, tenant_sender_addresses, ticket_categories, analyst_tenant_access, workflows, cost_centres, user_acts_for...), each a delete step. Everything else that names it is content, and content refuses the delete:

function recordDeleteCompanyContents(PDO $conn, int $id): array
{
    $own = array_keys(array_filter(recordDeleteSchemaRules('tenants'), fn($r) => $r === 'CASCADE'));
    // every tenant_id / *_tenant_id column on this install ...
        if ($t === 'tenants' || in_array($k, $own, true) || in_array($k, RECORD_DELETE_COMPANY_LOGS, true)) continue;

The preview returns it as contents; recordDeleteBlocked() answers not_empty while it has rows, and default_company for the Default one. Retire is tenants.is_active = 0. A company's bin entry belongs to the Default company (the deleted one is nobody's any more). Restore refuses if another company now has the same name.

⚠️ TRAP: a company reference is never cleared the way a person's is. For most records an empty tenant_id means the Default company - the schema's SET NULL on assets, tasks, changes... would quietly move them into Default. That is why the only way past is an empty company.

2. Preview, refusals, Retire

recordDeletePreview() changes nothing. It returns the plan as counts, retire (what Retire would do) or retire_why (already / no_status), blocked and warnings:

  • recordDeleteBlocked() - a person still is_managed by a directory source that is enabled and sync_enabled (the same test as DirectoryMaintenance::sourceImports()): the next run would create them again. Refused, naming the source.
  • recordDeleteWarnings() - an asset with asset_inventory_sources rows: deleted, with a warning that the next inventory run may create it again.
  • recordDeleteRetire() - an asset through AssetsService::updateFields() into the first active status with is_retired = 1 (so it gets its history row and retired_datetime); a person is_active = 0, deactivated_datetime set.

Company scope: recordDeleteLoad() answers not found for a record in a company the analyst cannot reach (never forbidden, which would confirm it exists); recordDeleteFind() and recordBinList() filter with allAccessibleTenantsFilter().

3. Settings

Key Where it is set Default Read by
system_settings.record_bin_days Delete records β†’ Bin β†’ Keep deleted records for (7 / 30 / 90 / 365) 30 recordBinDays() β†’ recordBinPurge(), recordBinList() (the "until" date)

4. The log

recordDeleteLog() writes one system_logs row (log_type = 'record_delete') per retire, delete (with each table.column action count) and restore.

Related

FreeITSM

Getting Started

Modules

Multi-tenancy (planned)

Responses to feedback

Blue sky thinking

Bugs resolved

Pull requests

Links

Clone this wiki locally