@@ -751,7 +751,10 @@ public ResponseEntity deleteProject(
751751 return ResponseEntity .badRequest ().body (RESPONSE_BODY_FOR_MODERATION_REQUEST_WITH_COMMIT );
752752 }
753753
754- // Check for clearing request and delete if it exists and is open
754+ // Fetch the clearing request before deletion attempt, but do NOT delete it yet.
755+ // The CR must only be removed after confirming the project deletion succeeded;
756+ // deleting it first and then discovering the project is IN_USE causes permanent
757+ // data loss — the project remains but its clearing history is gone.
755758 ThriftClients thriftClients = new ThriftClients ();
756759 ModerationService .Iface moderationClient = thriftClients .makeModerationClient ();
757760 ClearingRequest clearingRequest = null ;
@@ -762,26 +765,20 @@ public ResponseEntity deleteProject(
762765 log .info ("No clearing request found for project: " + id + " (exception: " + e .getMessage () + ")" );
763766 }
764767
765- if (clearingRequest != null && !isClosedClearingRequest (clearingRequest )) {
766- log .warn ("Project has an open clearing request. Attempting to delete the clearing request and then the project." );
767-
768- try {
769- RequestStatus clearingRequestStatus = moderationClient .deleteClearingRequest (clearingRequest .getId (), sw360User );
770- if (clearingRequestStatus != RequestStatus .SUCCESS ) {
771- log .error ("Failed to delete clearing request for project: " + id );
772- return new ResponseEntity <>(HttpStatus .INTERNAL_SERVER_ERROR );
773- }
774- log .info ("Successfully deleted the open clearing request for project: " + id );
775- } catch (TException e ) {
776- log .error ("Error deleting clearing request for project: " + id , e );
777- return new ResponseEntity <>(HttpStatus .INTERNAL_SERVER_ERROR );
778- }
779- } else if (clearingRequest != null ) {
780- log .info ("The clearing request for project is already closed. Proceeding with project deletion." );
781- }
782-
783768 RequestStatus requestStatus = projectService .deleteProject (id , sw360User );
784769 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+ }
785782 return new ResponseEntity <>(HttpStatus .OK );
786783 } else if (requestStatus == RequestStatus .IN_USE ) {
787784 return new ResponseEntity <>(HttpStatus .CONFLICT );
0 commit comments