@Transactional
    public void deleteContract(String organizationId, String clientId, String version, Long contractId)
        throws ClientNotFoundException, ContractNotFoundException, NotAuthorizedException,
        InvalidClientStatusException {

        try {
            ArrayList<ContractBean> allContracts = Lists.newArrayList(tryAction(() -> storage.getAllContracts(organizationId, clientId, version)));
            ContractBean contractToDelete = storage.getContract(contractId);
            if (allContracts.size() <= 1) {
                // If we are deleting the only/last contract, then we can unregister.
                ClientStatus clientStatus = contractToDelete.getClient().getStatus();
                if (clientStatus == ClientStatus.Registered || clientStatus == ClientStatus.AwaitingApproval) {
                    actionService.unregisterClient(organizationId, clientId, version);
                }
                deleteContractsInternal(organizationId, clientId, version, allContracts, List.of(contractToDelete));
            } else {
                // If we still have contracts left, we should re-register the existing client.
                ClientStatus newStatus = deleteContractsInternal(organizationId, clientId, version, allContracts, List.of(contractToDelete));
                if (newStatus == ClientStatus.Registered || newStatus == ClientStatus.AwaitingApproval) {
                    actionService.registerClient(organizationId, clientId, version);
                }
            }
        } catch (Exception e) {
            throw new SystemErrorException(e);
        }
    }
Check to see if the contract already exists, by getting a list of all the
client's contracts and comparing with the one being created.
manager/api/rest-impl/src/main/java/io/apiman/manager/api/service/ContractService.java