checking for null query_id on state when initialising query.

Fixes all issues in tickets #10851 & #10859
Merge pull request #1491 from opf/bugfix/clear-query-strangeness

Clearing query from service cache. Fixes all issues in tickets #10851 & ...