# Change an item's edges without losing changes (/build/write-edges-safely)

Change, remove or replace an item's edges so that no write undoes a link another app or agent made since you read them.



Another app, agent or device may add, change or remove an item's [edges](/build/edges) between your read and your write. **An edge has a version of its own, and an item's version doesn't count its edges** ([Versions](/build/versions#what-the-version-counts)). To guard a link, name the versions of the edges you read. In the TypeScript examples, `marfa` is a client made as [the TypeScript SDK](/reference/typescript-sdk#create-a-client) shows.

## Change or remove one edge [#change-or-remove-one-edge]

[Update an edge](/reference/api/edges/updateEdge) names the version you read, and **[Delete an edge](/reference/api/edges/deleteEdge) can too:**



**CLI**

```bash  
marfa edges delete <edge-id> --version 2
```

**HTTP**

```http
DELETE /edges/<edge-id>?version=2
Authorization: Bearer <key>
```

**TypeScript**

```ts
const { data, error } = await marfa.DELETE("/edges/{id}", {
  params: { path: { id: edgeId }, query: { version: 2 } },
});
```

If the edge changed since, Marfa **keeps it** and answers `409 version_conflict`, with the edge in `current`. Without a `version`, the delete removes the edge as it stands, changed or not.

## Replace an item's edges as you read them [#replace-an-items-edges-as-you-read-them]

[Update an item](/reference/api/items/updateItem) can send `edges`, which replaces the item's outgoing edges of each type it names with edges to the items listed. On its own, it also removes an edge another writer drew since you read the list.

To prevent that, **send `read_edges`:** for each type in `edges`, the edges of that type you read, each by its `id` and `version`. [List an item's edges](/reference/api/edges/listItemEdges) answers both. An empty list means the item had none. The order doesn't matter.



**HTTP**

```http  
PATCH /items/<id>
Authorization: Bearer <key>
Content-Type: application/json

{
  "version": 3,
  "edges": { "in-collection": ["01a1f139-e0a3-704f-8070-83f64acc3f15"] },
  "read_edges": {
    "in-collection": [{ "id": "01a1011c-5b2e-7c41-9a3d-2f6b8e4c1d07", "version": 1 }]
  }
}
```

**TypeScript**

```ts
const { data, error } = await marfa.PATCH("/items/{id}", {
  params: { path: { id } },
  body: {
    version: 3,
    edges: { "in-collection": ["01a1f139-e0a3-704f-8070-83f64acc3f15"] },
    read_edges: { "in-collection": [{ id: "01a1011c-5b2e-7c41-9a3d-2f6b8e4c1d07", version: 1 }] },
  },
});
```

If the item's edges of a type you listed are not the ones you read, **Marfa writes nothing** and answers `409 version_conflict`, with the item in `current`. That holds even when the write would change nothing, and with `conflict=auto`. Read the edges again, decide which links to keep, and send the write again.

Every type in `read_edges` must also be in `edges`. A type that isn't is refused `400 validation_error`, with `read_edges.<type>` in `details.field`. Only an update takes `read_edges`: a create and [a bulk write](/build/write-items-in-bulk) don't.

The rules are in the contract's [edges](https://github.com/withmarfa/marfa/blob/main/conformance/spec/edges.md#edgesinline-read-stale) chapter.
