How to Clear Cell Output in Jupyter Notebook [4 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Table of Contents

  1. Clear Cell Output while running code in Jupyter Notebook
  2. Clearing the Cell Output from the top menu in Jupyter Notebook
  3. Setting up a keyboard shortcut to clear cell output in Jupyter Notebook
  4. Clearing the cell output via the Command Palette in Jupyter Notebook

# Clear Cell Output while running code in Jupyter Notebook

Use the clear_output() method to clear the output of a cell while running code.

The method enables you to wait to clear the output until new output is available to replace it.

main.py
from IPython.display import clear_output for i in range(50): clear_output(wait=True) print('bobbyhadz.com')

clear cell output in for loop

The only argument we passed to the clear_output method is a boolean.

If set to True, then the method waits to clear the output until the new output is available to replace it.

Notice that only a single message is printed after iterating over the range 50 times.

Make sure to specify the message that you want to actually print after the call to the clear_output() method.

If you need to flush the output when calling print() set the flush argument to True.

main.py
from IPython.display import clear_output for i in range(50): clear_output(wait=False) print('bobbyhadz.com', flush=True)

set flush argument to true

# Clearing the Cell Output from the top menu in Jupyter Notebook

If you need to clear the cell output from the top menu:

  1. Click on Cell and hover over Current Outputs.

  2. Click on Clear.

clear cell output from top menu

Here is a short clip that demonstrates how this works.

clear cell output from top menu

Note that this only clears the output of the current cell.

If you need to clear the output of all cells:

  1. Click on Cell and hover over All Output.

  2. Click on Clear.

clear all cell output

Here is a short GIF that shows how this works.

clear all cell output

# Setting up a keyboard shortcut to clear cell output in Jupyter Notebook

If you need to set up a keyboard shortcut to clear the output of a cell:

  1. Click on Help in the top menu and then click Edit Keyboard Shortcuts.

edit keyboard shortcuts

  1. Press Ctrl + F (or Cmd + F on macOS) and search for clear.

  2. If you need to set a keyboard shortcut to clear the output of a single cell, look for the clear cell output command.

set keyboard shortcut clear cell output

  1. Click on the add shortcut input field and specify your preferred keyboard shortcut, e.g. Ctrl-4 or Ctrl-O.

  2. If you need to set a keyboard shortcut to clear the output of all cells, look for the clear all cells output command toward the bottom.

clear all cells output

  1. Set your preferred keyboard shortcut for the command.

  2. Once you set the keyboard shortcut, click on the OK button.

To use your keyboard shortcuts, click to the left of your cell so that the left border becomes blue and press your keyboard shortcut.

click to left of cell

To not have to click with your mouse, you can just press Ctrl + Enter to run the code in the cell so that the left border becomes blue and then press your keyboard shortcut.

clear cell output using keyboard shortcut

# Clearing the cell output via the Command Palette in Jupyter Notebook

You can also clear the cell output via the command palette:

  1. Press Ctrl + Shift + P (or Cmd + Shift + P on macOS).
  2. Type clear.

clear cell output using command palette

  1. Select clear all cells output if you want to clear the output of all cells.
  2. Select clear cell output if you only want to clear the output of a single cell.

I've also written a detailed guide on how to wrap code/text into multiple lines in Jupyter Notebook.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.