MySQL
process management
show processlist
kill processes
database administration

How do I kill all the processes in Mysql show processlist?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

MySQL sessions can accumulate and block throughput during long locks, forgotten clients, or runaway scripts. Killing all active processes is possible, but doing it blindly can disrupt replication, migrations, and legitimate workloads. The safer approach is to filter and terminate with explicit criteria.

A reliable implementation should remain understandable during troubleshooting and upgrades. That requires explicit assumptions, clear boundaries, and verifiable behavior under both normal and failure conditions.

Core Sections

1. Inspect process list before terminating sessions

Always review active sessions first to avoid terminating your own connection, replication users, or maintenance jobs. Collect enough context to identify patterns such as lock waits or idle transactions.

sql
1SHOW FULL PROCESSLIST;
2
3SELECT ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO
4FROM INFORMATION_SCHEMA.PROCESSLIST
5ORDER BY TIME DESC;

The baseline should be intentionally small and deterministic. A compact first version is easier to test, easier to reason about, and faster to review when teams iterate.

2. Generate targeted kill commands

Build kill statements for selected sessions, excluding system accounts. This gives precise control and reduces collateral impact.

sql
1SELECT CONCAT('KILL ', ID, ';') AS stmt
2FROM INFORMATION_SCHEMA.PROCESSLIST
3WHERE USER NOT IN ('system user', 'replication')
4  AND ID <> CONNECTION_ID()
5  AND COMMAND <> 'Sleep';
6
7-- review output, then execute approved statements

After baseline correctness, harden around edge cases and integration boundaries. Explicit validation, timeout handling, and predictable error semantics make downstream behavior safer.

3. Automate emergency cleanup carefully

If scripted cleanup is necessary, add guardrails such as allowlists, thresholds, and audit logging. Operational scripts should be conservative and reversible.

Operationally, define what success looks like in measurable terms and record baseline metrics before rollout. This makes post-change evaluation objective rather than anecdotal.

Include at least one representative production-like test, one malformed-input test, and one dependency-failure test in CI. Repeatable coverage prevents regressions introduced by dependency changes or refactors.

Keep ownership and escalation paths clear. When incidents happen, responders should know who owns the code path, what logs and metrics to inspect first, and how to execute a safe rollback or fallback mode.

Before release, confirm recovery mechanics in practice. A rollback strategy that is never rehearsed is often too slow under pressure, while a validated recovery workflow can reduce outage impact dramatically.

A complete engineering solution also includes explicit contracts for ownership, inputs, and failure semantics. Document what callers may send, which errors are retriable, and what actions operators should take when dependencies degrade. Clear contracts reduce ambiguity between teams and prevent divergent behavior in different services that rely on the same pattern.

Testing should represent real constraints rather than toy inputs only. Add one production-like scenario, one malformed-input scenario, and one dependency-failure scenario with deterministic assertions. Keep these checks in continuous integration so every change verifies behavior against the same baseline. This practice catches regressions early and reduces the chance of late surprises during rollout.

Observability should be focused and intentional. Emit concise logs for key branch decisions, include request identifiers for traceability, and track metrics tied to user impact such as latency percentiles, error rates, and retry outcomes. Focused telemetry helps teams distinguish application defects from infrastructure instability quickly during incidents.

Before deployment, prepare rollback and fallback options that can be executed quickly. Feature toggles, staged rollout, and a validated reversion workflow significantly reduce operational risk when real traffic reveals assumptions that were not visible in development. Recovery planning in advance is a core reliability practice and should be rehearsed periodically.

Finally, keep runbook notes near the implementation and update them as behavior evolves. Short, current documentation dramatically improves handoffs and lowers on-call resolution time.

Common Pitfalls

  • Killing all sessions without excluding current admin connection.
  • Terminating replication or backup sessions and causing data risk.
  • Ignoring lock root cause and repeatedly killing symptoms only.
  • Executing generated kill commands without review in production.
  • Running cleanup scripts without audit logs or approval controls.

Summary

  • Inspect process metadata before issuing kill commands.
  • Filter session termination using explicit safety criteria.
  • Exclude system and replication users by policy.
  • Pair cleanup actions with root-cause investigation.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design