Fixed: "CS0120: An object reference is required" for ClearAllPoolsAsync in MySql.Data
MySqlConnection.ClearAllPools() is static, so I wrote await MySqlConnection.ClearAllPoolsAsync(), and the compiler said CS0120. The async versions in MySql.Data aren't static, unlike the methods they mirror.
What I Was Doing
I was writing a small program to show how to clear the connection pool, on MySql.Data 26.10.0 and .NET 10:
await MySqlConnection.ClearAllPoolsAsync();
dotnet run clear-pool.cs
What I Expected
It to compile, as the method it mirrors does.
What I Got
/Users/you/projects/mysql-connection-test/clear-pool.cs(5,7): error CS0120: An object reference is required for the non-static field, method, or property 'MySqlConnection.ClearAllPoolsAsync(CancellationToken)'
The build failed. Fix the build errors and run again.

What I Checked
The class's members, in MySql.Data 26.10.0:
public static void ClearPool(MySqlConnection connection)
public Task ClearPoolAsync(MySqlConnection connection, CancellationToken cancellationToken = default)
public static void ClearAllPools()
public Task ClearAllPoolsAsync(CancellationToken cancellationToken = default)
The sync ones are static. The async ones aren't.
Why It Happens
CS0120 means you've called an instance method as though it were static. Here, the two pairs of methods just differ.
The Fix
Use the static methods. They're simple calls, and there's no reason to wait on them in most code:
MySqlConnection.ClearAllPools();
To clear one pool, pass a connection that has that connection string. It doesn't need to be open:
await using (var connection = new MySqlConnection(connectionString))
{
MySqlConnection.ClearPool(connection);
}
Or, if you do want the async versions, call them on an instance:
await using var connection = new MySqlConnection(connectionString);
await connection.ClearAllPoolsAsync();
After the Fix
dotnet run clear-pool.cs
First open: connection id 33
Second open: connection id 33
After ClearAllPools: connection id 34
After ClearPool: connection id 35
The id changes because each clear closed the pooled connection, so the next open needed a new one.

Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Adding new MySqlConnection().ClearAllPoolsAsync() everywhere | It works, and reads oddly | Use the static method |
| Clearing pools routinely | Every request then pays for a new connection | Only clear after a real change, such as a failover |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
CS0176 on the sync method through an instance | The reverse mistake | Call it on the class |
| Connections still there after clearing | Connections in use close when they're returned | Check again once they've finished |
Conclusion
ClearPool and ClearAllPools are static, and their async versions aren't. Call the static ones on the class.
Additional Resources
- Compiler Error CS0120 — what it means.
- Managing a Connection Pool in Connector/NET — the pool.
