def clear_sessions(hyperstream, inactive_only=True, clear_history=False):
        """
        Clear all (inactive) sessions and (optionally) their history
        
        :param hyperstream: The hyperstream object
        :param inactive_only: Whether to only clear inactive sessions (active sessions may be owned by another process)
        :param clear_history: Whether to clear the history of the session. Note that this will only clear the history 
        if the creator is the same process: there could feasibly be a history stored in a file channel that is not 
         accessible by this process.
        
        """
        query = dict()
        if inactive_only:
            query['active'] = False
        if hyperstream.current_session is not None:
            query['session_id__ne'] = hyperstream.current_session.session_id

        with switch_db(SessionModel, "hyperstream"):
            for s in SessionModel.objects(**query):
                if clear_history:
                    channel = hyperstream.channel_manager[s.history_channel]
                    stream_id = StreamId("session", meta_data=(('uuid', str(s.session_id)),))
                    try:
                        channel.purge_stream(stream_id, remove_definition=True, sandbox=None)
                    except StreamNotFoundError:
                        pass
                s.delete()