> ## 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 by Filters

> Delete all invoices matching filter criteria with concurrency guard

# Bulk Delete by Filters

Delete all invoices that match the given filter criteria. Uses the same filters as the list endpoint plus a `filter_hash` for optimistic concurrency — ensuring you delete exactly what the user saw.

## 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>

### Query Parameters

At least one filter parameter (beyond `facility_id` and `type`) is required.

<ParamField query="facility_id" type="string" required>
  UUID of the facility
</ParamField>

<ParamField query="type" type="string" required>
  Invoice type: `electricity`, `heat`, `water`, `recharge`
</ParamField>

<ParamField query="start_date" type="string">
  Filter invoices on or after this date
</ParamField>

<ParamField query="end_date" type="string">
  Filter invoices on or before this date
</ParamField>

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

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

<ParamField query="cups[]" type="string[]">
  Filter by CUPS identifier
</ParamField>

<ParamField query="supplier_id[]" type="string[]">
  Filter by supplier UUID
</ParamField>

### Body Parameters

<ParamField body="filter_hash" type="string" required>
  Hash from the list endpoint response. Must match the current filter set.
</ParamField>

## Example

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  # Step 1: Get filter_hash from list
  RESPONSE=$(curl -s "https://api.dcycle.io/v1/invoices?facility_id=${FACILITY_ID}&type=electricity&status[]=error" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}")

  FILTER_HASH=$(echo $RESPONSE | jq -r '.filter_hash')

  # Step 2: Bulk delete with the same filters
  curl -X POST "https://api.dcycle.io/v1/invoices/bulk-delete-by-filters?facility_id=${FACILITY_ID}&type=electricity&status[]=error" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}" \
    -H "Content-Type: application/json" \
    -d "{\"filter_hash\": \"${FILTER_HASH}\"}"
  ```

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

  headers = {
      "x-api-key": os.getenv("DCYCLE_API_KEY"),
      "x-organization-id": os.getenv("DCYCLE_ORG_ID"),
  }

  filters = {
      "facility_id": os.getenv("FACILITY_ID"),
      "type": "electricity",
      "status[]": "error",
  }

  # Step 1: Get filter_hash
  list_resp = requests.get("https://api.dcycle.io/v1/invoices", headers=headers, params=filters)
  filter_hash = list_resp.json()["filter_hash"]

  # Step 2: Bulk delete
  delete_resp = requests.post(
      "https://api.dcycle.io/v1/invoices/bulk-delete-by-filters",
      headers={**headers, "Content-Type": "application/json"},
      params=filters,
      json={"filter_hash": filter_hash},
  )

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

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

  const headers = {
    'x-api-key': process.env.DCYCLE_API_KEY,
    'x-organization-id': process.env.DCYCLE_ORG_ID,
  };

  const filters = {
    facility_id: process.env.FACILITY_ID,
    type: 'electricity',
    'status[]': 'error',
  };

  // Step 1: Get filter_hash
  const listResp = await axios.get('https://api.dcycle.io/v1/invoices', { headers, params: filters });
  const filterHash = listResp.data.filter_hash;

  // Step 2: Bulk delete
  const deleteResp = await axios.post(
    'https://api.dcycle.io/v1/invoices/bulk-delete-by-filters',
    { filter_hash: filterHash },
    { headers: { ...headers, 'Content-Type': 'application/json' }, params: filters },
  );

  const { success_count, failed_count } = deleteResp.data;
  console.log(`Deleted: ${success_count} | Failed: ${failed_count}`);
  ```
</CodeGroup>

### Successful Response

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "success_count": 2,
  "success_ids": [
    "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "b2c3d4e5-f6a7-8901-bcde-f12345678901"
  ],
  "failed_count": 0,
  "failed_ids": [],
  "message": "Successfully deleted 2 invoices"
}
```

## Response

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

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

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

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

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

## Common Errors

### 401 Unauthorized

**Cause:** Missing or invalid API key

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

### 403 Forbidden

**Cause:** The authenticated user is not a member of the organization

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{"detail": "Logged User is not Member of Organization", "code": "LOGGED_USER_NOT_MEMBER"}
```

### 409 Conflict

**Cause:** `filter_hash` doesn't match — filters changed since the list was loaded

```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."
}
```

### 422 Unprocessable Entity

**Cause:** No narrowing filter provided beyond `facility_id` and `type`

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

## Related Endpoints

<CardGroup cols={2}>
  <Card title="Bulk Delete" icon="trash" href="/api-reference/invoices/bulk-delete">
    Delete specific invoices by ID
  </Card>

  <Card title="Bulk Delete Preview" icon="eye" href="/api-reference/invoices/bulk-delete-preview">
    Preview what will be deleted
  </Card>
</CardGroup>
