> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/feature-management-experimentation/management-and-administration/api-best-practices/examples/delete-keys.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# Delete thousands of keys in a segment

Learn how to delete segment keys in chunks using the Admin API.

## Overview <a href="#overview" id="overview"></a>

Use this script to delete segment keys in chunks when a segment contains thousands of user keys.

## Prerequisites <a href="#prerequisites" id="prerequisites"></a>

* Install the [Python Admin API Wrapper](https://developer.harness.io/feature-management-experimentation/management-and-administration/api-best-practices/wrappers/python-admin-api.md).
* You have created an Admin API key from the Harness FME UI.

## Configuration <a href="#configuration" id="configuration"></a>

Before running the script, update the following variables in the code:

* `ADMIN API KEY`: Your Admin API key.
* `workspaceName`: The name of the workspace. If you only have one workspace, use the Default workspace.
* `environmentName`: The name of the environment.
* `segmentName`: The name of the segment.

Run this script using Python 3 from your local machine or preferred development environment.

```python
from splitapiclient.main import get_client

#############################################
workspaceName = "Defaults"
environmentName = "Production"
segmentName = "employees"
#############################################

client = get_client({'apikey': 'ADMIN API KEY'})
ws = client.workspaces.find(workspaceName)
env = client.environments.find(environmentName, ws.id)
segDef = client.segment_definitions.find(segmentName, env.id, ws.id)

segKeys = segDef.get_keys()
cnt=0
keys = []
for key in segKeys:
    cnt = cnt + 1
    if cnt > 90:
        final = {"keys": keys, "comment":"removing 1k keys"}
        segDef.remove_keys(final)
        keys = []
        cnt = 0
    keys.append(key)
if cnt < 90:
    final = {"keys": keys, "comment":"removing 1k keys"}
    segDef.remove_keys(final)
```

## Next steps

* Go to [Import thousands of customer keys to an existing segment](https://developer.harness.io/feature-management-experimentation/management-and-administration/api-best-practices/examples/import-keys.md) to import keys into a segment.
* Go to [Copy segment IDs to another segment](https://developer.harness.io/feature-management-experimentation/management-and-administration/api-best-practices/examples/copy-segment.md) to copy keys to another segment.

{% @harness-feedback/feedback %}
