Handle deletion cascade (organizations, cameras, poses, sequences, alerts) and storage cleanup

Open
#541 5 comments 1 reaction 1 assignee View on GitHub

Maintainers usually reply within 7 days

@Majyles is already working on this.

Since Nov 9, 2026.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
fastapi, postgresql, python

Research direction

Review PR #538 and the organization deletion flow around organizations.session, including the alert-association deletion shown in the issue. Trace how cameras, poses, sequences, detections, and associated S3 media relate to the organization and its foreign-key constraints. Done means organization deletion removes all listed records and media without orphaned data, with the coverage test completed.

Written by the indexing model from the issue text.

Description

endpoint: organizations module: database

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()
Dominant language
Python
Stars
31
Forks
15
Avg merge
7d 18h
Merged PRs (30d)
17

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pyronear/pyro-api

All issues in pyronear/pyro-api

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.