> ## 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 business travels matching the given filters

# Bulk Delete by Filters

Deletes all business travels matching the provided filter parameters. Requires at least one filter to prevent accidental mass deletion. A `filter_hash` from the list endpoint must be provided to confirm the filters haven't changed since the user reviewed them.

<Warning>
  **Permanent Action**: Deleted records and their associated emissions cannot be recovered.
</Warning>

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

<ParamField header="Content-Type" type="string" required>
  Must be `application/json`
</ParamField>

### Query Parameters

Same filter parameters as the [list endpoint](/api-reference/business-travels/list):

<ParamField query="transport_type[]" type="array[string]">
  Filter by transport type

  **Available values:** `car`, `metro`, `train`, `trolleybus`, `bus`, `motorbike`, `aircraft`, `ferry`
</ParamField>

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

  **Available values:** `active`, `pending`, `loading`, `completed`, `error`
</ParamField>

<ParamField query="start_date" type="date">
  Filter by minimum travel date (inclusive)

  **Format:** `YYYY-MM-DD`
</ParamField>

<ParamField query="end_date" type="date">
  Filter by maximum travel date (inclusive)

  **Format:** `YYYY-MM-DD`
</ParamField>

<ParamField query="name" type="string">
  Search by name (case-insensitive substring match)
</ParamField>

<ParamField query="name[]" type="array[string]">
  Filter by exact name values (multi-select, OR logic)
</ParamField>

<ParamField query="file_id[]" type="array[uuid]">
  Filter by source file UUID
</ParamField>

<ParamField query="created_at_from" type="datetime">
  Filter records created on or after this timestamp

  **Format:** `YYYY-MM-DDTHH:MM:SSZ`
</ParamField>

<ParamField query="created_at_to" type="datetime">
  Filter records created on or before this timestamp

  **Format:** `YYYY-MM-DDTHH:MM:SSZ`
</ParamField>

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

  **Available values:** `calculated`, `not_calculated`
</ParamField>

### Body Parameters

<ParamField body="filter_hash" type="string" required>
  Hash returned by the list endpoint for the current filter combination. Prevents deleting records from a stale filter state.
</ParamField>

## Response

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

<ResponseField name="success_ids" type="array[string]">
  UUIDs of business travels that were successfully deleted
</ResponseField>

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

<ResponseField name="failed_ids" type="array[string]">
  UUIDs that could not be deleted (not found or deletion error)
</ResponseField>

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

## Example

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  # Step 1: List with filters to get the filter_hash
  FILTER_HASH=$(curl -s -X GET "https://api.dcycle.io/v1/business-travels?transport_type[]=aircraft&start_date=2024-01-01&end_date=2024-12-31" \
    -H "x-api-key: ${DCYCLE_API_KEY}" \
    -H "x-organization-id: ${DCYCLE_ORG_ID}" | jq -r '.filter_hash')

  # Step 2: Delete all matching records
  curl -X POST "https://api.dcycle.io/v1/business-travels/bulk-delete-by-filters?transport_type[]=aircraft&start_date=2024-01-01&end_date=2024-12-31" \
    -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 = {
      "transport_type[]": ["aircraft"],
      "start_date": "2024-01-01",
      "end_date": "2024-12-31",
  }

  # Step 1: Get the filter_hash
  list_response = requests.get(
      "https://api.dcycle.io/v1/business-travels",
      headers=headers,
      params=filters,
  )
  filter_hash = list_response.json()["filter_hash"]

  # Step 2: Delete all matching records
  response = requests.post(
      "https://api.dcycle.io/v1/business-travels/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 headers = {
    'x-api-key': process.env.DCYCLE_API_KEY,
    'x-organization-id': process.env.DCYCLE_ORG_ID,
  };
  const filters = {
    'transport_type[]': ['aircraft'],
    start_date: '2024-01-01',
    end_date: '2024-12-31',
  };

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

  // Step 2: Delete matching records
  const { data: result } = await axios.post(
    'https://api.dcycle.io/v1/business-travels/bulk-delete-by-filters',
    { filter_hash: listData.filter_hash },
    { headers: { ...headers, 'Content-Type': 'application/json' }, params: filters }
  );

  console.log(`Deleted: ${result.success_count}, Failed: ${result.failed_count}`);
  console.log(result.message);
  ```
</CodeGroup>

### Successful Response

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "success_count": 42,
  "success_ids": [
    "550e8400-e29b-41d4-a716-446655440000",
    "550e8400-e29b-41d4-a716-446655440001"
  ],
  "failed_count": 0,
  "failed_ids": [],
  "message": "Bulk delete completed: 42 deleted, 0 failed"
}
```

## 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:** 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 filter parameters provided

```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/business-travels/bulk-delete">
    Delete specific records by ID
  </Card>

  <Card title="List" icon="list" href="/api-reference/business-travels/list">
    Browse and filter business travels
  </Card>

  <Card title="Delete" icon="trash-can" href="/api-reference/business-travels/delete">
    Delete a single business travel
  </Card>
</CardGroup>
