complete the logic for command cleanup