SQL Server › SQL Server Profiler & Extended Events

How do you properly stop and clean up an Extended Events session once it is no longer needed?

Intermediate

Answer

You use the ALTER EVENT SESSION statement with STATE = STOP to stop the session from capturing further events, and optionally use DROP EVENT SESSION to remove its definition entirely if you do not plan to use it again, ensuring you are not leaving unnecessary monitoring sessions running and consuming resources indefinitely.
ALTER EVENT SESSION LongRunningQueries ON SERVER STATE = STOP;
DROP EVENT SESSION LongRunningQueries ON SERVER;
Real-world example A team stops and removes a temporary Extended Events session they had created for a specific troubleshooting investigation, cleaning up once the issue was resolved and the session was no longer needed.

Common follow-ups: What happens to already captured data in an event file after the session is stopped?;How do you list all currently active Extended Events sessions on a server?