Skip to content

Handle organization deletion cascade for cameras, sequences, detections, and storage cleanup #541

@MateoLostanlen

Description

@MateoLostanlen

Description
Deleting an organization currently only removes alerts in order to satisfy foreign key constraints. Cameras, poses, sequences, detections, and associated media stored in S3 are not deleted. This leaves orphaned data and makes the data lifecycle unclear.

Before alerts were handled, deletion was already incomplete for cameras and related objects, so this issue existed even earlier and is not specific to alerts only.

I started addressing this partially in PR #538 by explicitly deleting alerts and their association tables, but coverage test became quite comple so I stopped for this PR and opened this issue to address it properly later

# Remove alerts and their associations for this organization to satisfy FK constraints
org_session = organizations.session
alert_ids_res = await org_session.exec(
    select(Alert.id).where(Alert.organization_id == organization_id)
)
alert_ids = list(alert_ids_res.all())

if alert_ids:
    delete_links = delete(AlertSequence).where(
        cast(Any, AlertSequence.alert_id).in_(alert_ids)
    )
    delete_alerts = delete(Alert).where(
        cast(Any, Alert.id).in_(alert_ids)
    )
    await org_session.exec(delete_links)
    await org_session.exec(delete_alerts)
    await org_session.commit()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions