# Deleting data from data sources

### Deleting data from a CSV Data Source

You can delete data from a CSV data source on the fly within your simulation just as if your API were deleting data from a real data source. The data is removed for the duration that the service is running. When the service is restarted, the data will revert to what was in the original uploaded CSV.

There are two ways to delete data.

1. Using the {{csvDeleteRows}} function which will delete rows where a specified column matches a specified value
2. Using the {{csvSqlCommand}} function which allows you to use a SQL like syntax to issue a DELETE SQL command which allows more conditions to be matched than csvDeleteRows.

For illustration we will use this data source called "pets":

| id   | category | name   | status    |
| ---- | -------- | ------ | --------- |
| 1000 | birds    | Archie | available |
| 1001 | dogs     | Zipper | available |
| 1002 | dogs     | Teddy  | sold      |

### Method 1: Delete data with {{csvDeleteRows}}

This method deletes data where a column matches a value that you specify. It can also optionally return the number of rows deleted into the template, or into another function wrapping it.

Syntax:

```handlebars
{{csvDeleteRows 'data-source-name' 'column-name' 'query-value' output-result}}
```

Template Example: Delete all birds and don't return the count

```handlebars
{{ csvDeleteRows 'pets' 'category' 'birds' false }}
```

Result:

In this case there will be no output from csvDeleteRows but the data in memory for all subsequent calls to the API simulation will be modified as follows:

| id   | category | name   | status    |
| ---- | -------- | ------ | --------- |
| 1001 | dogs     | Zipper | available |
| 1002 | dogs     | Teddy  | sold      |

If the output-result flag had been set to true, then the function would have had the following output:

```
1
```

###

### Method 2: Delete data with {{csvSqlCommand}}

A simplified SQL like syntax can be used to delete the data in a csv data source. (This syntax can also be used to [SELECT ](/create-simulations/using-data-sources-in-simulations/querying-data-sources.md)and [UPDATE ](/create-simulations/using-data-sources-in-simulations/updating-data-in-data-sources.md)from the data source. You cannot insert using this syntax however there is a mechanism which can be read about [here](/create-simulations/using-data-sources-in-simulations/inserting-data-into-a-data-source.md).)

You can read more about using this SQL like syntax [here](/create-simulations/using-data-sources-in-simulations/guidance-on-using-the-sql-like-syntax.md).

Syntax:

```handlebars
{{ csvSqlCommand 'sql-delete-statement' }}
```

Template Example: Delete all dogs whose ids are greater than 1001

```handlebars
{{ csvSqlCommand "DELETE FROM pets WHERE category = 'dogs' AND id > '1001'" }}
```

Result:

There is no output from a SQL DELETE statement but the data in memory for all subsequent calls to the API simulation will be modified as follows:

| id   | category | name   | status    |
| ---- | -------- | ------ | --------- |
| 1000 | birds    | Archie | sold      |
| 1001 | dogs     | Zipper | available |

### s


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cloud.hoverfly.io/create-simulations/using-data-sources-in-simulations/deleting-data-from-data-sources.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
