If the judge or magistrate directs the action to be dismissed, the defendant must, if in custody, be discharged therefrom; or if admitted to bail, his bail is exonerated, or money deposited instead of bail must be refunded to him or to the person or persons found by the court to have deposited said money on behalf of said defendant.