Do you need to delete all Markdown / non Markdown cells from Jupyter? In this short guide, we'll see how to delete programmatically cells in Jupyter Notebook or JupyterLab.

Step 1: Delete All Markdown Cells in Jupyter

The first example will show you how to delete all Markdown cells in the current notebook - Markdown.ipynb:

import nbformat as nbf
ntbk = nbf.read("Markdown.ipynb", nbf.NO_CONVERT)
new_ntbk = ntbk
new_ntbk.cells = [cell for cell in ntbk.cells if cell.cell_type != "markdown"]
nbf.write(new_ntbk, "MarkdownDeleted.ipynb", version=nbf.NO_CONVERT)

New Notebook is created where Markdown cells are deleted. The same notebook can be used but this can lead to code loss.

The code above will keep any cell which is not Markdown.

Note: If you need to recover deleted information from your Jupyter Notebooks please check: 6 ways to Recover deleted Jupyter Notebook (including JupyterLab)

Step 2: Delete All Cells Except Markdown in Jupyter

What if you want to keep only the Markdown cells? Then you can change the code above to:

import nbformat as nbf
ntbk = nbf.read("Markdown.ipynb", nbf.NO_CONVERT)
new_ntbk = ntbk
new_ntbk.cells = [cell for cell in ntbk.cells if cell.cell_type == "markdown"]
nbf.write(new_ntbk, "OnlyMarkdown.ipynb", version=nbf.NO_CONVERT)

Note: Similar logic can be applied for Code/Raw cells which are marked as cell_type - code or raw

Pro tip: If you want to delete cells based on input then you can change: if cell.cell_type == "markdown" to cell.input

Step 3: Delete Cells based on Index and Content with JavaScript

It's possible to use JavaScript code in order to search cells based on their content and index in order to remove cells from your Notebook. The code snippet below shows how you can delete all cells which had index higher than 5 which contain text: Delete me or Test Cell:

from IPython.core.display import display, HTML, Javascript
display(
    Javascript(
        """
    var cells = IPython.notebook.get_cells();
    cells.forEach(function(cell) {
        if(cell.get_text().includes('Delete me') || cell.get_text().includes('Test Cell')) {
            var index = IPython.notebook.find_cell_index(cell);
            if(index > 1){
                IPython.notebook.delete_cell(index);
            }
        }
    });
"""
    )
)

You need to place this code inside your Notebook and then execute it. Have in mind that this might delete the cell itself as well.

Step 4: Jupyter Delete Cell with Shortcut

If you need to delete the cells one by one then you can use a shortcut in order to speed up the process. So to delete a single cell you can:

  • click on the cell number area
  • Press D - two times

Another way is by using a different approach. While you are working on the cell:

  • press Escape
  • then press X

This will cut the cell. Later if you want you can paste it by pressing V

Note: Selecting the cell number or escaping from the input focus allow the usage of shortcuts.

Note 2: If you like to see all Jupyter Notebook shortcuts then you can do it by:

  • Help
  • Keyboard Shortcuts

Step 5: Jupyter Delete Multiple Cells with Shortcut

The final example is about selection and deletion of multiple cells at once with Jupyter Notebook. So first we need to select all cells which will be deleted:

  • click on the cell number ( the left part of the cell input)
  • click on another cell while pressing Shift
  • Finally press X or press two times D