# delete()

Canonical URL: https://developers.iagenify.com/sdk/ref/files/delete/
Language: en

`api.files.delete(sourceId) → Promise<Result>`

Removes the file, its record, and whatever was indexed from it for semantic search.

Status: DELETE /api/files/:id · free · not reversible

## Parameters {#parameters}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `sourceId` | string | required | From [list()](/sdk/ref/files/list). Missing → the SDK throws. |

## Returns {#returns}

```javascript
{
  ok: true,
  message: "…",
  sourceId: "…",
  removedChunks: 12     // how many indexed fragments went with it
}
```

`removedChunks` is the useful one: it tells you how much of the semantic index this file was, and `0` means it was never indexed — which is normal for anything that is not a `document`.

## What goes {#what}

|  | After delete() |
| --- | --- |
| The bytes | gone |
| The record | gone — get() answers 404 |
| Its indexed fragments | gone, so search stops returning it |
| The storage's remaining space | given back |
| A local copy you saved with path | untouched, on your disk |

> **There is no trash**
>
> Read it out first if it mattered — reading is free — or keep the local copy that save({ path }) can write for you at the same time.

```javascript
const file = await api.files.get(sourceId);
await fs.writeFile(`./archive/${file.name}`, file.content);
await api.files.delete(sourceId);
```

## Errors {#errors}

| Code | Status | When |
| --- | --- | --- |
| — (thrown locally) | — | No sourceId passed. |
| file_not_found | 404 | No file of this account with that id — including one already deleted. |

So deleting twice answers `404` the second time. When the goal was simply that the file be gone, treat that as success.

```javascript
import { IaGenifyError } from "iagenify";

async function ensureGone(api, sourceId) {
  try {
    await api.files.delete(sourceId);
    return "deleted";
  } catch (err) {
    if (err instanceof IaGenifyError && err.status === 404) return "already gone";
    throw err;
  }
}
```

## Deleting is how you take something out of search {#search}

Updating a file does not re-index it, so a correction made with `update()` leaves the old text findable. When a document must stop being findable as it was, delete it and save the new version.

- [file.update()](/sdk/ref/files/update): Why replacing content is not always enough.
- [run · search](/sdk/ref/connected-storage/run/search): What the index is for.

```python
{
    "ok": True,
    "message": "…",
    "sourceId": "…",
    "removedChunks": 12,     # how many indexed fragments went with it
}
```

```python
from pathlib import Path

file = api.files.get(source_id)
content = file.content.encode() if isinstance(file.content, str) else file.content
Path("archive", file.name).write_bytes(content)
api.files.delete(source_id)
```

```python
from iagenify import IaGenifyError

def ensure_gone(api, source_id):
    try:
        api.files.delete(source_id)
        return "deleted"
    except IaGenifyError as error:
        if error.status == 404:
            return "already gone"
        raise
```
