@@ -765,28 +765,38 @@ public ResponseEntity deleteProject(
765765 log .info ("No clearing request found for project: " + id + " (exception: " + e .getMessage () + ")" );
766766 }
767767
768+ // Delete the project first. If this fails the clearing request is left
769+ // completely untouched — no data loss.
768770 RequestStatus requestStatus = projectService .deleteProject (id , sw360User );
769- if (requestStatus == RequestStatus .SUCCESS ) {
770- if (clearingRequest != null && !isClosedClearingRequest (clearingRequest )) {
771- try {
772- RequestStatus clearingRequestStatus = moderationClient .deleteClearingRequest (clearingRequest .getId (), sw360User );
773- if (clearingRequestStatus != RequestStatus .SUCCESS ) {
774- log .error ("Project deleted but failed to delete open clearing request for project: " + id );
775- } else {
776- log .info ("Successfully deleted the open clearing request for project: " + id );
777- }
778- } catch (TException e ) {
779- log .error ("Project deleted but error while deleting clearing request for project: " + id , e );
780- }
781- }
782- return new ResponseEntity <>(HttpStatus .OK );
783- } else if (requestStatus == RequestStatus .IN_USE ) {
771+ if (requestStatus == RequestStatus .IN_USE ) {
784772 return new ResponseEntity <>(HttpStatus .CONFLICT );
785773 } else if (requestStatus == RequestStatus .SENT_TO_MODERATOR ) {
786774 return new ResponseEntity <>(RESPONSE_BODY_FOR_MODERATION_REQUEST , HttpStatus .ACCEPTED );
787- } else {
775+ } else if ( requestStatus != RequestStatus . SUCCESS ) {
788776 throw new SW360Exception ("Something went wrong." );
789777 }
778+
779+ // Project is gone. Now remove the open clearing request if one existed.
780+ // The backend already orphaned it during project cleanup, so deleting by ID
781+ // is still safe at this point.
782+ if (clearingRequest != null && !isClosedClearingRequest (clearingRequest )) {
783+ try {
784+ RequestStatus crStatus = moderationClient .deleteClearingRequest (clearingRequest .getId (), sw360User );
785+ if (crStatus != RequestStatus .SUCCESS ) {
786+ log .warn ("Project {} deleted but clearing request {} could not be removed" ,
787+ id , clearingRequest .getId ());
788+ return ResponseEntity .ok ()
789+ .body ("Project deleted successfully, but the associated clearing request could not be removed." );
790+ }
791+ log .info ("Clearing request {} deleted after project {}" , clearingRequest .getId (), id );
792+ } catch (TException e ) {
793+ log .warn ("Project {} deleted but clearing request cleanup threw an exception" , id , e );
794+ return ResponseEntity .ok ()
795+ .body ("Project deleted successfully, but the associated clearing request could not be removed." );
796+ }
797+ }
798+
799+ return new ResponseEntity <>(HttpStatus .OK );
790800 }
791801
792802 private boolean isClosedClearingRequest (ClearingRequest clearingRequest ) {
0 commit comments