Deleting jobsets first would fail because buildmetrics has an FK to the jobset. However, the jobset / project relationship is not marked as CASCADE. Deleting all the builds automatically cascades to delete buildmetrics, so deleting the relevant builds first, then deleting the jobset solves it.