static void wolfSSH_SFTP_ClearState(WOLFSSH* ssh, enum WS_SFTP_STATE_ID state)
{
    if (ssh) {

        if (state == 0)
            state = (enum WS_SFTP_STATE_ID)~state; /* set all bits hot */

        if (state & STATE_ID_GET) {
            if (ssh->getState) {
                WFREE(ssh->getState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->getState = NULL;
            }
        }

        if (state & STATE_ID_LSTAT) {
            if (ssh->lstatState) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->lstatState->buffer);
                WFREE(ssh->lstatState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->lstatState = NULL;
            }
        }

        if (state & STATE_ID_OPEN) {
            if (ssh->openState) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->openState->buffer);
                WFREE(ssh->openState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->openState = NULL;
            }
        }

        if (state & STATE_ID_SEND_READ) {
            if (ssh->sendReadState) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->sendReadState->buffer);
                WFREE(ssh->sendReadState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->sendReadState = NULL;
            }
        }

        if (state & STATE_ID_CLOSE) {
            if (ssh->closeState) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->closeState->buffer);
                WFREE(ssh->closeState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->closeState  = NULL;
            }
        }

        if (state & STATE_ID_GET_HANDLE) {
            if (ssh->getHandleState) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->getHandleState->buffer);
                WFREE(ssh->getHandleState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->getHandleState = NULL;
            }
        }

        if (state & STATE_ID_NAME) {
            if (ssh->nameState) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->nameState->buffer);
                WFREE(ssh->nameState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->nameState = NULL;
            }
        }

        if (state & STATE_ID_SEND) {
            if (ssh->sendState) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->sendState->buffer);
                WFREE(ssh->sendState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->sendState = NULL;
            }
        }

        if (state & STATE_ID_LS) {
            if (ssh->lsState) {
                WFREE(ssh->lsState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->lsState = NULL;
            }
        }

        if (state & STATE_ID_READDIR) {
            if (ssh->readDirState) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->readDirState->buffer);
                WFREE(ssh->readDirState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->readDirState = NULL;
            }
        }

        if (state & STATE_ID_PUT) {
            if (ssh->putState) {
                WFREE(ssh->putState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->putState = NULL;
            }
        }

        if (state & STATE_ID_SEND_WRITE) {
            if (ssh->sendWriteState) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->sendWriteState->buffer);
                WFREE(ssh->sendWriteState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->sendWriteState = NULL;
            }
        }

        if (state & STATE_ID_RM) {
            if (ssh->rmState != NULL) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->rmState->buffer);
                WFREE(ssh->rmState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->rmState = NULL;
            }
        }

        if (state & STATE_ID_MKDIR) {
            if (ssh->mkdirState != NULL) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->mkdirState->buffer);
                WFREE(ssh->mkdirState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->mkdirState = NULL;
            }
        }

        if (state & STATE_ID_RMDIR) {
            if (ssh->rmdirState != NULL) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->rmdirState->buffer);
                WFREE(ssh->rmdirState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->rmdirState = NULL;
            }
        }

        if (state & STATE_ID_RENAME) {
            if (ssh->renameState != NULL) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->renameState->buffer);
                WFREE(ssh->renameState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->renameState = NULL;
            }
        }

        if (state & STATE_ID_RECV) {
            if (ssh->recvState != NULL) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->recvState->buffer);
                WFREE(ssh->recvState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->recvState = NULL;
            }
        }

        if (state & STATE_ID_CHMOD) {
            if (ssh->chmodState) {
                WFREE(ssh->chmodState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->chmodState = NULL;
            }
        }

        if (state & STATE_ID_SETATR) {
            if (ssh->setatrState) {
                wolfSSH_SFTP_buffer_free(ssh, &ssh->setatrState->buffer);
                WFREE(ssh->setatrState, ssh->ctx->heap, DYNTYPE_SFTP_STATE);
                ssh->setatrState = NULL;
            }
        }
    }
}