# Delete key-value pairs

To remove a key-value pair from KV Store, call the `delete()` method on your 
KV instance:

```javascript
await kv.delete(key);
```

## Example

An example of deleting a key-value pair from within a Function:

```javascript
async function handleRequest(request) {
  const kv = new Azion.KV();

  try {
    await kv.delete("user-session");

    return new Response("Key deleted successfully", { status: 200 });
  } catch (e) {
    return new Response(e.message, { status: 500 });
  }
}

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});
```

---

## Reference

### `delete()` method

To remove a key-value pair, call the `delete()` method on your KV instance:

```javascript
await kv.delete(key);
```

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `key` | `string` | The key of the KV pair to delete. |

#### Response

| Response | Type | Description |
|----------|------|-------------|
| `response` | `Promise<void>` | A Promise that resolves when the deletion is complete. |

The `delete()` method returns a Promise that you should `await` to verify 
successful deletion.

---

## Guidance

### Deleting non-existent keys

Deleting a key that doesn't exist will not throw an error. The operation 
completes successfully regardless of whether the key existed:

```javascript
async function handleRequest(request) {
  const kv = new Azion.KV();

  // This will succeed even if the key doesn't exist
  await kv.delete("non-existent-key");

  return new Response("Delete operation completed", { status: 200 });
}

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});
```

### Eventual consistency

Like all KV Store operations, deletions are eventually consistent. After 
deleting a key:

- The deletion is immediately visible in the same global network location.
- Other locations may still see the old value for up to 60 seconds (or the 
  duration of any `cacheTtl` that was set when reading the key).

### Conditional deletion

You may want to check if a key exists before deleting it, or perform 
additional logic:

```javascript
async function handleRequest(request) {
  const kv = new Azion.KV();
  const key = "user-session";

  // Check if key exists before deleting
  const value = await kv.get(key, "text");

  if (value === null) {
    return new Response("Key not found", { status: 404 });
  }

  // Perform deletion
  await kv.delete(key);

  return new Response("Key deleted", { status: 200 });
}

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});
```

### Deleting multiple keys

To delete multiple keys, you can use `Promise.all()`:

```javascript
async function handleRequest(request) {
  const kv = new Azion.KV();

  const keysToDelete = ["key1", "key2", "key3"];

  await Promise.all(keysToDelete.map(key => kv.delete(key)));

  return new Response("All keys deleted", { status: 200 });
}

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});
```

### Deleting namespaces

To delete an entire namespace (not just a key), use the static 
`Azion.KV.delete()` method:

```javascript
async function handleRequest(request) {
  const namespaceName = "my-namespace";

  // Delete the entire namespace
  await Azion.KV.delete(namespaceName);

  return new Response("Namespace deleted", { status: 200 });
}

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event.request));
});
```

:::caution
Deleting a namespace removes all key-value pairs within it. This operation 
cannot be undone.
:::

---

## Related

- [Write key-value pairs](/en/documentation/runtime/api-reference/kv-store/write/)
- [Read key-value pairs](/en/documentation/runtime/api-reference/kv-store/read/)
- [KV Store API overview](/en/documentation/runtime/api-reference/kv-store/)