

# DELETE FROM dbclustergroup\_sys.data\_mappings

Use `DELETE FROM dbclustergroup_sys.data_mappings` to remove the router name of a database or a table in a database cluster group for Aurora.

## Description
<a name="databaseclustergroups-sql-delete-data-mappings-description"></a>

Removes data mappings. The delete doesn't change the object on the member cluster. After the delete reaches the routing layer, the routing layer no longer knows the router name, and a statement that names it returns a not-found error.

For the task, see [Changing data mappings](databaseclustergroups-changing-mappings.md).

## Syntax
<a name="databaseclustergroups-sql-delete-data-mappings-syntax"></a>

```sql
DELETE FROM dbclustergroup_sys.data_mappings
 WHERE { {{column}} = '{{value}}' | ({{column}}, {{column}} [, ...]) = ('{{value}}', '{{value}}' [, ...]) };
```

## Parameters and options
<a name="databaseclustergroups-sql-delete-data-mappings-parameters"></a>

The statement takes the following parameters.

WHERE  
One equality, or one tuple equality, on the router name columns or on a shared value. A database-level row has an empty string, not `NULL`, in its schema and table columns, so an empty string selects only that row. On Aurora MySQL, `(router_db_name, router_table_name) = ('{{db}}', '')` selects the database-level mapping.

## Where it runs and required privileges
<a name="databaseclustergroups-sql-delete-data-mappings-privileges"></a>

Run the statement where you run every mapping write: at the routing endpoint, in an unpinned session, outside a transaction. For the engine rules, see [INSERT INTO dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-insert-data-mappings.md#databaseclustergroups-sql-insert-data-mappings-privileges).

The router user needs `dbclustergroup_metadata_admin` or `dbclustergroup_super_admin`. For all privileges for database cluster groups for Aurora, see [Privileges](databaseclustergroups-sql-reference.md#databaseclustergroups-sql-privileges).

## Usage notes
<a name="databaseclustergroups-sql-delete-data-mappings-usage"></a>

Keep the following in mind when you use the statement.
+ A mapping change reaches the routing layer within a short time, and a statement routed on an older mapping returns a retryable error. See [Applying data mapping changes](databaseclustergroups-data-mappings.md#databaseclustergroups-when-a-data-mapping-change-takes-effect).
+ Match exactly the rows that you mean, and check the number of affected rows. A `WHERE` on `router_collocation_group` or `member_cluster_alias` deletes every row with that value. A `DELETE` whose `WHERE` clause matches more than 1,000 rows fails with error 1235 and removes nothing. A `DELETE` without a `WHERE` clause removes every mapping that no scaleout table uses, up to 1,000 rows for each statement, table-level mappings first. Each repeat removes the next rows. It keeps the mappings that scaleout tables use, and when only those are left, it fails with error 1235. For more information about the limit, see [Routing metadata and naming limits](databaseclustergroups-limitations.md#databaseclustergroups-limitations-routing-metadata-limits). The routing layer also refuses `LIKE` in the `WHERE` clause. Export the mappings first. For more information about exporting, see [Exporting the mappings before and after a change](databaseclustergroups-changing-mappings.md#databaseclustergroups-export-mappings).
+ The routing layer refuses the delete of a mapping that a scaleout table still names as a member. Drop the scaleout table first. See [DROP SCALEOUT TABLE](databaseclustergroups-sql-drop-scaleout-table.md).
+ To change a database from database-level to table-level mappings, delete the database-level row and insert the table-level rows directly after it. The database is unreachable through the routing endpoint between the two steps. See [Using database-level and table-level mappings](databaseclustergroups-mapping-levels.md).

**Note**  
You can't delete a mapping that a data movement job holds a lock on. The statement fails with error 1205 (HY000), `Metadata for … is being changed by another operation`, and removes nothing. A `DELETE` without a `WHERE` clause fails the same way when any data movement job holds a lock. Wait until `cleanup_status` reads `Complete` in `move_info`, and then run the statement again.

**Note**  
The delete needs an available writer instance on each member cluster that holds a mapping that it deletes. If a member cluster has no available writer, the delete fails and removes nothing. The routing layer reaches member clusters directly, not through the routing endpoint.

## Examples
<a name="databaseclustergroups-sql-delete-data-mappings-examples"></a>

The following example deletes the database-level mapping of `app_db`.

```sql
DELETE FROM dbclustergroup_sys.data_mappings
 WHERE (router_db_name, router_table_name) = ('app_db', '');
```

## Errors
<a name="databaseclustergroups-sql-delete-data-mappings-errors"></a>

The statement can return the following errors. For the error texts and all errors from the routing layer, see [Routing and data mapping errors](databaseclustergroups-errors-routing.md).


| Aurora MySQL | Meaning | 
| --- | --- | 
| [1227](databaseclustergroups-errors-routing.md) (42000) | The router user lacks dbclustergroup\_metadata\_admin. | 
| [1235](databaseclustergroups-errors-routing.md) (42000) | The write is inside a transaction. Commit or roll back, and send it again outside a transaction. | 
| [1235](databaseclustergroups-errors-scaleout.md) (42000) | cannot remove data mapping '<db>.<table>': referenced by a scaleout table: the mapping backs a scaleout table. Drop the scaleout table first. | 
| [1235](databaseclustergroups-errors-routing.md) (42000) | cannot remove all data mappings: <n> rows are still referenced by scaleout tables: a DELETE without a WHERE clause found only mappings that scaleout tables use, so it removed nothing. To remove those mappings, drop your scaleout tables first. | 
| [1235](databaseclustergroups-errors-routing.md) (42000) | Remove of <n> rows exceeds the per-request limit of 1000; narrow the filter or split the write into smaller requests: your WHERE clause matches more than 1,000 rows, so the statement removes nothing. Narrow the WHERE clause. | 
| [1205](databaseclustergroups-errors-routing.md) (HY000) | Metadata for … is being changed by another operation: a data movement job holds a lock on the mapping, or on any mapping for a DELETE without a WHERE clause, so the statement removes nothing. Run it again after the job's cleanup completes. | 
| [1235](databaseclustergroups-errors-routing.md) (42000) | AND/OR are not supported in the WHERE clause on this table: the WHERE clause has more than one condition. Use one equality or the tuple form. | 
| [1412](databaseclustergroups-errors-routing.md) (HY000) | After a mapping change, a statement that the routing layer routed on the older mapping returns this error. On Aurora MySQL, the routing layer resends a text-protocol statement up to 3 times first, then returns 1412 with the session and any open transaction intact, so retry the statement. | 
| [63928](databaseclustergroups-errors-routing.md) (40001) | Inside a transaction, a statement needed a mapping that changed after the transaction's first routed statement, and the routing layer rolled the whole transaction back. Retry the whole transaction from its first statement; don't send COMMIT. | 

## Related statements
<a name="databaseclustergroups-sql-delete-data-mappings-related"></a>

For more information, see the following:
+ [SELECT FROM dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-select-data-mappings.md)
+ [INSERT INTO dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-insert-data-mappings.md)
+ [UPDATE dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-update-data-mappings.md)
+ [ALTER SCALEOUT TABLE](databaseclustergroups-sql-alter-scaleout-table.md)
+ [DROP SCALEOUT TABLE](databaseclustergroups-sql-drop-scaleout-table.md)