# KILL

## Syntax

```sql
KILL [HARD | SOFT] { {CONNECTION|QUERY} thread_id | QUERY ID query_id | USER user_name }
```

## Description

Each connection to mariadbd runs in a separate thread. You can see which threads are running with the `SHOW PROCESSLIST` statement and kill a thread with the `KILL thread_id` statement.`KILL` allows the optional `CONNECTION` or`QUERY` modifier:

* `KILL CONNECTION` is the same as `KILL` with no modifier: It terminates the connection associated with the given thread or query id.
* `KILL QUERY` terminates the statement that the connection `thread_id` is currently executing, but leaves the connection itself intact.
* `KILL QUERY ID` terminates the query by `query_id`, leaving the connection intact.

If a connection is terminated that has an active transaction, the transaction is rolled back. If only a query is killed, the current transaction stays active. See also [idle\_transaction\_timeout](https://mariadb.com/docs/server/server-management/variables-and-modes/server-system-variables#idle_transaction_timeout).

If you have the [PROCESS](https://mariadb.com/docs/server/reference/account-management-sql-statements/grant#process) privilege, you can see all threads.

{% tabs %}
{% tab title="Current" %}
If you have the [CONNECTION ADMIN](https://mariadb.com/docs/server/reference/account-management-sql-statements/grant#connection-admin) privilege, you can kill all threads and statements. Otherwise, you can see and kill only your own threads and statements.
{% endtab %}

{% tab title="< 10.5.2" %}
If you have the [SUPER](https://mariadb.com/docs/server/reference/account-management-sql-statements/grant#super) privilege, the [CONNECTION ADMIN](https://mariadb.com/docs/server/reference/account-management-sql-statements/grant#connection-admin) privilege, you can kill all threads and statements. Otherwise, you can see and kill only your own threads and statements.
{% endtab %}
{% endtabs %}

{% hint style="warning" %}
Killing queries that repair or create indexes on MyISAM and Aria tables may result in corrupted tables. Use the `SOFT` option to avoid this.
{% endhint %}

The `HARD` option (default) kills a statement as soon as possible. If you use`SOFT`, critical operations that may leave a table in an inconsistent state are not interrupted. Such operations include `REPAIR` and `INDEX` creation for [MyISAM](https://mariadb.com/docs/server/server-usage/storage-engines/myisam-storage-engine) and [Aria](https://mariadb.com/docs/server/server-usage/storage-engines/aria) tables ([REPAIR TABLE](https://mariadb.com/docs/server/reference/sql-statements/table-statements/repair-table), [OPTIMIZE TABLE](https://mariadb.com/docs/server/ha-and-performance/optimization-and-tuning/optimizing-tables/optimize-table)).

`KILL ... USER username` kills all connections and queries for a given user. `USER` can be specified in one of the following ways:

* username (kill without regard to hostname)
* username\@hostname
* [CURRENT\_USER](https://mariadb.com/docs/server/reference/sql-functions/secondary-functions/information-functions/current_user) or [CURRENT\_USER()](https://mariadb.com/docs/server/reference/sql-functions/secondary-functions/information-functions/current_user)

If you specify a thread id and that thread does not exist, you get the following error:

```
ERROR 1094 (HY000): Unknown thread id: <thread_id>
```

If you specify a query id that doesn't exist, you get the following error:

```
ERROR 1957 (HY000): Unknown query id: <query_id>
```

However, if you specify a user name, no error is issued for non-connected (or even non-existing) users. To check if the connection/query has been killed, you can use the [ROW\_COUNT()](https://mariadb.com/docs/server/reference/sql-functions/secondary-functions/information-functions/row_count) function.

A client whose connection is killed receives the following error:

```
ERROR 1317 (70100): Query execution was interrupted
```

To obtain a list of existing sessions, use the [SHOW PROCESSLIST](https://mariadb.com/docs/server/reference/sql-statements/administrative-sql-statements/show/show-processlist) statement or query the [Information Schema](https://mariadb.com/docs/server/reference/system-tables/information-schema) [PROCESSLIST](https://mariadb.com/docs/server/reference/system-tables/information-schema/information-schema-tables/information-schema-processlist-table) table.

{% hint style="info" %}
You cannot use `KILL` with the Embedded MariaDB Server library because the embedded server merely runs inside the threads of the host application. It does not create any connection threads of its own.
{% endhint %}

**Note:** You can also use `mariadb-admin kill thread_id [,thread_id...]` to kill connections. To get a list of running queries,use `mariadb-admin processlist`. See [mariadb-admin](https://mariadb.com/docs/server/clients-and-utilities/administrative-tools/mariadb-admin).

## See Also

* [Query limits and timeouts](https://mariadb.com/docs/server/ha-and-performance/optimization-and-tuning/query-optimizations/query-limits-and-timeouts)
* [Aborting statements that exceed a certain time to execute](https://mariadb.com/docs/server/ha-and-performance/optimization-and-tuning/query-optimizations/aborting-statements)
* [idle\_transaction\_timeout](https://mariadb.com/docs/server/server-management/variables-and-modes/server-system-variables#idle_transaction_timeout)

<sub>*This page is licensed: GPLv2, originally from*</sub> [<sub>*fill\_help\_tables.sql*</sub>](https://github.com/MariaDB/server/blob/main/scripts/fill_help_tables.sql)

{% @marketo/form formId="4316" %}
