---
title: "Delete"
method: POST
path: "/api/new_mdm/delete"
tags: ["MDM NEW"]
---

# Delete

`POST /api/new_mdm/delete`

**Function**: `Delete`  
**Type**: API Endpoint (POST)

**Purpose**:  
The function handles the deletion of records from the customer database based on the provided query and conditions. It ensures that records are backed up before deletion, maintaining a log of the operation.

**URL**:  
`POST /api/new_mdm/delete`

**Request Headers**:
- `customer_key` (str, required): An encoded string containing the `lscode` and `firmcode` for the customer. This must be decoded for database access.

**Request Body**:
```json
{
    "query": "<query>",
    "type": <int>
}
```

**Response Example**:
```json
{
    "status": "Success",
    "message": ""
}
```

**Steps**:
1. **Log Creation**:  
A unique log ID is generated based on the current date and time, ensuring traceability for each request.

2. **Customer Key Decoding**:  
The `customer_key` is decoded using base64. If the decoded value is invalid or improperly formatted, the function returns an error response.

3. **Request Validation**:  
The function checks if the required fields (`query`) are present in the request body.

4. **Database Connection**:  
A connection to the customer-specific database is established using the `custCursorConnection` function with the decoded `lscode` and `firmcode`.

5. **SQL Query Execution**:  
A SQL query is executed to first back up the records to be deleted, then delete them from the main table, handling any exceptions that may occur.

6. **Response Construction**:  
The results from the query execution are formatted and returned as a JSON response.

**Error Handling**:
- **Invalid Customer Key**:  
If the decoded customer key is empty or does not follow the expected format, an appropriate error message is returned.
- **Invalid Request Body**:  
If any required fields in the request body are missing, an error response is returned.
- **Database Errors**:  
Any exceptions during query execution are caught, logged, and a `500` status response is returned with error details.

**Security Considerations**:
- The `customer_key` is encoded to protect sensitive information like `lscode` and `firmcode`.

**Error Response Example**:
```json
{
    "status": "failed",
    "message": "Invalid Customer Key"
}
```

```json
{
    "status": "failed",
    "message": "Query should not be null"
}
```

```json
{
    "status": "failed",
    "message": {
        "error_type": "OperationalError",
        "message": "SQL syntax error"
    }
}
```

## Query parameters

- `customer_key` string, required

## Request body

- MdmDeleteRecord
  - `query` string, required
  - `type` integer, required

## Response `200`

Successful Response

- unknown

## Other responses

- `422` — Validation Error

---

[API](https://skmtc.dev/leapsurgebi/apis/leapsurgeapi.md) · [All operations](https://skmtc.dev/leapsurgebi/apis/leapsurgeapi/llms.txt) · [OpenAPI document](https://skmtc-service-production.skmtc.workers.dev/v1/apis/leapsurgebi/leapsurgeapi/revisions/5a6b1464bfeb/schema)
