I process SelectQuery object using hook_query_TAG_after() or hook_query_alter().

I want to remove 'mytag' tag, which has been added as $query->addTag('mytag') somewhere else. Apparently, neither SelectQuery, nor QueryAlterableInterface has appropriate interface method.

Is there any means (besides SelectQuery subclassing) that I can use to remove a tag?

The reason may be the following (thanks Clive for asking):

I want to make a decision based on (expected) record set size inside hook_query_TAG_after()

I could do $countQuery = $query->countQuery(); $total = $countQuery->execute()->fetchField()

But, the cloned query will have same tag as original one and execute() will invoke the same hook ad infinitum.

  • Why do you need to remove it? Could you not just ignore it based on exactly the same condition that would have to be satisfied in order to remove it?
    – Clive
    Mar 20, 2012 at 11:20

2 Answers 2


I still can not find a way to remove a tag from query, but I found solution for my case of count query. The idea is to use another tag to stop endless recursion:

function mymodule_query_mytag_alter(QueryAlterableInterface $query) {

  if ($query->hasTag('counted')) {

  // Prepare count query
  $countQuery = $query->countQuery();
  $countQuery->addTag('counted'); // Abort hook recursion

  $tables =& $countQuery->getTables(); // Query may have sub-queries

  foreach ($tables as &$table) { // Tag all subqueries
    if ($table['table'] instanceof SelectQueryInterface) {
      $table['table']->addTag('counted'); // Abort hook recursion

  $total = $countQuery->execute()->fetchField(); // execute() will initiate recursion!

 // Rest of processing


You should use something like this:

    $new_query = clone($db_query);
    if (empty($new_query->alterTags)) {
    $search_api_current_entities = $new_query->execute()->fetchAll();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.