'Remove a cmdset from the  handler.
Args:
cmdset (CommandSet or str, optional): This can can be supplied either as a cmdset-key,
an instance of the CmdSet or a python path to the cmdset.
If no key is given, the last cmdset in the stack is
removed. Whenever the cmdset_stack changes, the cmdset is
updated. If default_cmdset is set, this argument is ignored.
default_cmdset (bool, optional): If set, this will remove the
default cmdset (at the bottom of the stack).'

def remove(self, cmdset=None, default_cmdset=False):

    if default_cmdset:
        if self.cmdset_stack:
            cmdset = self.cmdset_stack[0]
            if cmdset.permanent:
                storage = (self.obj.cmdset_storage or [''])
                storage[0] = ''
                self.obj.cmdset_storage = storage
            self.cmdset_stack[0] = _EmptyCmdSet(cmdsetobj=self.obj)
        else:
            self.cmdset_stack = [_EmptyCmdSet(cmdsetobj=self.obj)]
        self.update()
        return
    if (len(self.cmdset_stack) < 2):
        return
    if (not cmdset):
        cmdset = self.cmdset_stack.pop()
        if cmdset.permanent:
            storage = self.obj.cmdset_storage
            storage.pop()
            self.obj.cmdset_storage = storage
    else:
        if (callable(cmdset) and hasattr(cmdset, 'path')):
            delcmdsets = [cset for cset in self.cmdset_stack[1:] if (cset.path == cmdset.path)]
        else:
            delcmdsets = [cset for cset in self.cmdset_stack[1:] if ((cset.path == cmdset) or (cset.key == cmdset))]
        storage = []
        if any((cset.permanent for cset in delcmdsets)):
            storage = self.obj.cmdset_storage
            updated = False
            for cset in delcmdsets:
                if cset.permanent:
                    try:
                        storage.remove(cset.path)
                        updated = True
                    except ValueError:
                        pass
            if updated:
                self.obj.cmdset_storage = storage
        for cset in delcmdsets:
            try:
                self.cmdset_stack.remove(cset)
            except ValueError:
                pass
    self.update()
