> ## Documentation Index
> Fetch the complete documentation index at: https://code.dcycle.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Bulk Delete Wastes by Filters

> Delete all waste records matching the given filter criteria in a single operation

# Bulk Delete Wastes by Filters

Delete all waste records that match a set of filter criteria. This endpoint uses a two-step workflow: first fetch the waste list with filters applied (which returns a `filter_hash`), then call this endpoint with that hash to confirm you are deleting exactly what you saw.

<Warning>
  **Permanent Action**: Deleting waste records is permanent and cannot be undone. All associated emissions data will be removed from your organization's totals.
</Warning>

## How It Works

1. Call `GET /v1/wastes` with your desired filters — the response includes a `filter_hash` field.
2. Call this endpoint with the same query filters **and** pass the `filter_hash` in the request body.
3. The API verifies the hash matches the current filter results to prevent stale-data race conditions.

## Request

### Headers

<ParamField header="x-api-key" type="string" required>
  Your API key for authentication

  **Example:** `sk_live_1234567890abcdef`
</ParamField>

<ParamField header="x-organization-id" type="string" required>
  Your organization UUID

  **Example:** `a8315ef3-dd50-43f8-b7ce-d839e68d51fa`
</ParamField>

### Body Parameters

<ParamField body="filter_hash" type="string" required>
  The hash returned in the `filter_hash` field of the waste list response. Confirms you are deleting exactly the records you saw.

  **Example:** `"a3f1c2e4b5d6a7b8"`
</ParamField>

### Query Parameters

`facility_id` is required. At least one additional filter parameter is required.

<ParamField query="facility_id" type="string" required>
  The UUID of the facility to scope the deletion to

  **Example:** `660e8400-e29b-41d4-a716-446655440000`
</ParamField>

<ParamField query="status[]" type="array[string]">
  Filter by record status

  **Available values:** `active`, `uploaded`, `success`, `loading`, `pending`, `in_progress`, `review`, `in_review`, `error`

  **Example:** `status[]=error&status[]=loading`
</ParamField>

<ParamField query="start_date" type="string">
  Filter records with a start date on or after this date (YYYY-MM-DD)

  **Example:** `2024-01-01`
</ParamField>

<ParamField query="end_date" type="string">
  Filter records with an end date on or before this date (YYYY-MM-DD)

  **Example:** `2024-12-31`
</ParamField>

<ParamField query="file_id[]" type="array[string]">
  Filter by source file UUIDs (e.g. to delete all records imported from a specific file)
</ParamField>

<ParamField query="created_at_from" type="string">
  Filter records created on or after this datetime (ISO 8601)

  **Example:** `2024-01-01T00:00:00Z`
</ParamField>

<ParamField query="created_at_to" type="string">
  Filter records created on or before this datetime (ISO 8601)

  **Example:** `2024-12-31T23:59:59Z`
</ParamField>

<ParamField query="co2e_status" type="string">
  Filter by CO2e calculation status

  **Available values:** `calculated`, `not_calculated`

  **Example:** `co2e_status=not_calculated`
</ParamField>

<ParamField query="identification_name[]" type="array[string]">
  Filter by waste identification names or invoice numbers
</ParamField>

<ParamField query="ler_code[]" type="array[string]">
  Filter by LER (European Waste List) codes
</ParamField>

<ParamField query="rd_code[]" type="array[string]">
  Filter by RD (recovery/disposal) codes
</ParamField>

## Response

Returns `200 OK` with a JSON summary of the operation.

<ResponseField name="success_count" type="integer">
  Number of waste records successfully deleted
</ResponseField>

<ResponseField name="success_ids" type="array[string]">
  UUIDs of successfully deleted waste records
</ResponseField>

<ResponseField name="failed_count" type="integer">
  Number of records that failed to delete
</ResponseField>

<ResponseField name="failed_ids" type="array[string]">
  UUIDs of records that failed to delete
</ResponseField>

<ResponseField name="message" type="string">
  Human-readable summary of the operation
</ResponseField>

## Example

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  # Step 1: get list with filters to obtain filter_hash
  curl -X GET "https://api.dcycle.io/v1/wastes?facility_id=660e8400-e29b-41d4-a716-446655440000&status[]=error" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}"

  # Step 2: bulk delete using the filter_hash from the list response
  curl -X POST "https://api.dcycle.io/v1/waste/bulk-delete-by-filters?facility_id=660e8400-e29b-41d4-a716-446655440000&status[]=error" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}" \
    -H "Content-Type: application/json" \
    -d '{"filter_hash": "a3f1c2e4b5d6a7b8"}'
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import requests
  import os

  api_key = os.getenv("DCYCLE_API_KEY")
  org_id = os.getenv("DCYCLE_ORG_ID")

  headers = {
      "x-api-key": api_key,
      "x-organization-id": org_id,
      "Content-Type": "application/json"
  }

  filters = {
      "facility_id": "660e8400-e29b-41d4-a716-446655440000",
      "status[]": ["error"]
  }

  # Step 1: fetch list to get filter_hash
  list_response = requests.get(
      "https://api.dcycle.io/v1/wastes",
      headers=headers,
      params=filters
  )
  filter_hash = list_response.json()["filter_hash"]

  # Step 2: bulk delete with hash confirmation
  response = requests.post(
      "https://api.dcycle.io/v1/waste/bulk-delete-by-filters",
      headers=headers,
      params=filters,
      json={"filter_hash": filter_hash}
  )

  result = response.json()
  print(f"Deleted: {result['success_count']}, Failed: {result['failed_count']}")
  print(result["message"])
  ```

  ```javascript JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  const axios = require('axios');

  const apiKey = process.env.DCYCLE_API_KEY;
  const orgId = process.env.DCYCLE_ORG_ID;

  const headers = {
    'x-api-key': apiKey,
    'x-organization-id': orgId,
    'Content-Type': 'application/json'
  };

  const filters = {
    facility_id: '660e8400-e29b-41d4-a716-446655440000',
    'status[]': ['error']
  };

  // Step 1: fetch list to get filter_hash
  axios.get('https://api.dcycle.io/v1/wastes', { headers, params: filters })
    .then(listResponse => {
      const filterHash = listResponse.data.filter_hash;

      // Step 2: bulk delete with hash confirmation
      return axios.post(
        'https://api.dcycle.io/v1/waste/bulk-delete-by-filters',
        { filter_hash: filterHash },
        { headers, params: filters }
      );
    })
    .then(response => {
      const result = response.data;
      console.log(`Deleted: ${result.success_count}, Failed: ${result.failed_count}`);
      console.log(result.message);
    })
    .catch(error => console.error(error));
  ```
</CodeGroup>

### Successful Response

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "success_count": 12,
  "success_ids": [
    "550e8400-e29b-41d4-a716-446655440000",
    "660e8400-e29b-41d4-a716-446655440001"
  ],
  "failed_count": 0,
  "failed_ids": [],
  "message": "Successfully deleted 12 wastes"
}
```

## Common Errors

### 401 Unauthorized

**Cause:** Missing or invalid API key

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": "Invalid API key",
  "code": "INVALID_API_KEY"
}
```

### 409 Conflict — Filter Hash Mismatch

**Cause:** The `filter_hash` does not match the current filter results. The underlying data changed between the list call and the delete call.

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": "Filter hash mismatch. The filters have changed since the list was loaded. Please refresh and try again."
}
```

**Solution:** Re-fetch the waste list with the same filters to get a fresh `filter_hash`, then retry.

### 422 Unprocessable Entity — No Filters Provided

**Cause:** Only `facility_id` was provided. At least one additional filter is required to prevent accidental mass deletion.

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": "At least one filter parameter is required for bulk delete by filters."
}
```

### 422 Validation Error

**Cause:** Invalid query parameter value

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "detail": [
    {
      "loc": ["query", "status[]"],
      "msg": "value is not a valid enumeration member",
      "type": "type_error.enum"
    }
  ]
}
```

## Related Endpoints

<CardGroup cols={2}>
  <Card title="List Wastes" icon="list" href="/api-reference/wastes/list">
    Retrieve waste records and obtain the filter\_hash
  </Card>

  <Card title="Bulk Delete Wastes" icon="trash" href="/api-reference/wastes/bulk-delete">
    Delete specific waste records by ID
  </Card>

  <Card title="Unique Values" icon="chart-bar" href="/api-reference/wastes/unique-values">
    Get unique values for waste fields
  </Card>

  <Card title="Create Waste" icon="plus" href="/api-reference/wastes/create">
    Add a new waste record
  </Card>
</CardGroup>
