Can't return a value after Apoc.do.when() runs

I am trying to merge a user node and return true if the user node was created and false if the user node already existed. And if optional properties come in with the query, add them to the db.
Here is my query:

  CALL apoc.merge.node($labels, $identityProps, {created: true}, {created: false})
  YIELD node AS u
  WITH u
  CALL apoc.do.when(
    $optionalProps.advertisingId IS NOT NULL,
    "SET u.limitAdTracking = $optionalProps.limitAdTracking
    MERGE (a:AdvertisingId {advertisingId: $optionalProps.advertisingId})
    MERGE (u)-[:HAS_ADVERTISING_ID]->(a)",
    "",
    {u:u, optionalProps: $optionalProps}
    )
  YIELD value
  WITH u.created AS created, u
  REMOVE u.created
  RETURN created

As you can see, I set a temporary property on the user node in the first line, then assign that value to a return value called created, and then delete the temporary property before returning.

When I don't send $optionalProps, it works as expected: I get a return value of {created: boolean} and the created property on the user node is deleted at the end of the query.

When I do send $optionalProps (and $optionalProps.advertisingId is not null), It creates the user node, adds the created property, runs the ifQuery, all as expected , but then it returns nothing and doesn't delete u.created.
It seems like if apoc.do.when runs the ifQuery, I can't get it to do anything after the apoc.do.when. If I change the last few lines to
YIELD value RETURN value
I get nothing back.
same with
YIELD value RETURN u

I'm pretty stumped.

I'm using the javascript driver 5.1.0 like this (but I get the same behavior in the neo4j browser):

return await session.executeWrite((tx) => tx.run(query, {labels, identityProps, optionalProps));

Try returning something from each branch of the apoc.do.when procedure. You can try 'return 1', since you already have the node 'u'. Also, the preferred method of removing an attribute is to set its value to 'null'

that works, thanks!

I thought the ifQuery would just be a part of one query with the rest of it, but looks like it is sort of a separate operation? Or are there other situations where you can have two RETURNs in one query?

The apoc procedure will execute your write statements, but will not return anything if there is no return statement. As such, value will be null and will cause the query to stop and you will see '(no changes, no records)' in the browser or no result when using a driver.