Introduction

In today's digital age, hyperlinks have become an integral part of our online experience. Whether we're browsing the web or working on documents, hyperlinks make it easy to navigate and access information. However, there are times when we need to remove all hyperlinks in a document swiftly. Fear not, as we unveil the secrets to quickly removing those pesky hyperlinks in Docs.

In Microsoft Office, there is a feature that allows you to remove all hyperlinks at once with a shortcut. Docs lacks this feature so it's time-consuming to remove hyperlinks one by one manually. This article shows you a few options to quickly remove all hyperlinks in Docs.

Method 1: Use Office Word

This is a dumb method, perhaps you don't want to hear. You could

  1. Export the Doc to Word .docx by File > Download as > Microsoft Word.
  2. Open the .docx. Select the text with hyperlinks.
  3. Press shortcut Ctrl + Shift + F9.
  4. The hyperlinks will go away.
  5. Clean the hyperlink underlines with Format Painter.

Method 2: Use Apps script

This method works well but more technical.

  1. Copy the apps script to your Docs script editor with a name Remove all hyperlinks. If you don't know how to do it, here is a tutorial how to add an apps script to your Docs.
  2. Select the text in which you want to remove all hyperlinks. It doesn't have to be the entire document.
  3. Click menu Add-ons > Remove all hyperlinks > Remove all hyperlinks.
  4. The hyperlinks shall be removed while the format is kept.
Remove all hyperlinks in Docs with the Apps script below
Remove all hyperlinks in Docs with the Apps script below
/**
 * @license MIT
 *
 * © 2020 xfanatical.com. All Rights Reserved.
 * @since 1.0.0 remove links
 * @since 1.0.1 fixed a typo 
 */
 
function onOpen() {
  DocumentApp.getUi()
    .createAddonMenu()
    .addItem('Remove all hyperlinks', 'init')
    .addToUi()
}

/**
 * Get an array of all LinkUrls in the element
 * @param {Element} element The document element to operate on. 
 * @returns {Array}  Array of objects,
 * {
 *   element,
 *   startOffset,
 *   endOffsetInclusive, 
 *   url
 * }
 * credit to https://gist.github.com/mogsdad/6518632
 */
function getAllLinks(element, selectionStartOffset, selectionEndOffsetInclusive) {
  var links = []

  var type = element.getType()
  if (type === DocumentApp.ElementType.TEXT) {
    var textObj = element.editAsText()
    var text = element.getText()
    var inUrl = false
    
    var firstStartOffset = 0
    if (selectionStartOffset !== -1) {
      firstStartOffset = selectionStartOffset
    }
    
    var lastEndOffsetInclusive = text.length
    if (selectionEndOffsetInclusive !== -1) {
      lastEndOffsetInclusive = selectionEndOffsetInclusive + 1
    }
    
    for (var ch = firstStartOffset; ch < lastEndOffsetInclusive; ch++) {
      var url = textObj.getLinkUrl(ch)
      if (url != null) {
        if (!inUrl) {
          // We are now!
          inUrl = true
          var curUrl = {}
          curUrl.element = element
          curUrl.url = url
          curUrl.startOffset = ch
        } else {
          curUrl.endOffsetInclusive = ch
          if (ch === text.length - 1) {
            // this hyperlink is the end of the TEXT element
            inUrl = false
            links.push(curUrl)
            curUrl = {}
          }
        }          
      } else {
        if (inUrl) {
          // Not any more, we're not.
          inUrl = false
          links.push(curUrl)  // add to links
          curUrl = {}
        }
      }
    }
  }
  
  var singletonElement
  if (type === DocumentApp.ElementType.INLINE_IMAGE) {
    singletonElement = element.asInlineImage()
  } else if (type === DocumentApp.ElementType.EQUATION) {
    singletonElement = element.asEquation()
  } else if (type === DocumentApp.ElementType.EQUATION_FUNCTION) {
    singletonElement = element.asEquationFunction()
  } else if (type === DocumentApp.ElementType.LIST_ITEM) {
    singletonElement = element.asListItem()
  } else if (type === DocumentApp.ElementType.PARAGRAPH) {
    singletonElement = element.asParagraph()
  } else if (type === DocumentApp.ElementType.TABLE) {
    singletonElement = element.asTable()
  } else if (type === DocumentApp.ElementType.TABLE_CELL) {
    singletonElement = element.asTableCell()
  } else if (type === DocumentApp.ElementType.TABLE_OF_CONTENTS) {
    singletonElement = element.asTableOfContents()
  } else if (type === DocumentApp.ElementType.TABLE_ROW) {
    singletonElement = element.asTableRow()
  } else if (type === DocumentApp.ElementType.TEXT) {
    singletonElement = element.asText()
  }
  
  if (singletonElement) {
    var link = singletonElement.getLinkUrl()
    if (link) {
      links.push({
        element: singletonElement,
        url: link,
      })
    }
  }

  if (element.getNumChildren) {
    var numChildren = element.getNumChildren()
    for (var i = 0; i < numChildren; i++) {
      links = links.concat(getAllLinks(element.getChild(i), selectionStartOffset, selectionEndOffsetInclusive))
    }
  }

  return links
}

function init() {
  var doc = DocumentApp.getActiveDocument()
  var selection = doc.getSelection()
  if (selection) {
    var elements = selection.getRangeElements()
    for (var i = 0; i < elements.length; i++) {
      var rangeElement = elements[i]
      var element = rangeElement.getElement()
      
      if (element.editAsText) {
        var text = element.editAsText()
        
        var links = getAllLinks(element, rangeElement.getStartOffset(), rangeElement.getEndOffsetInclusive())
        for (var j = 0; j < links.length; j++) {
          var link = links[j]
          if (link.element.setLinkUrl) {
            if (link.hasOwnProperty('startOffset')) {
              link.element.setLinkUrl(link.startOffset, link.endOffsetInclusive, '')
            } else {
              link.element.setLinkUrl(null)
            }
          }
        }
      }
      
    }
  } else {
    DocumentApp.getUi().alert('Select the text with hyperlinks to be removed')
  }
}

Method 3: Use Add-on

If you found here, you're lucky. There is a free add-on on the market called Text Cleaner which does the job too. It's more convenient and hassle free. The instruction is almost as same as method 2 except the menu Text Cleaner > Remove links and underlining.

Remove all hyperlinks with add-on Text Cleaner
Remove all hyperlinks with add-on Text Cleaner

Hope it works for you. Feel free to leave any comments. Thanks for reading.

Conclusion

In the realm of document editing, hyperlinks undoubtedly play a vital role. However, there are instances when the removal of hyperlinks becomes necessary. Whether you opt for the manual approach, utilize keyboard shortcuts, leverage the power of Find and Replace, or explore add-ons, the choice ultimately depends on the size and complexity of your document. By following the techniques outlined in this guide, you can swiftly remove all hyperlinks in Docs and unleash the true potential of your documents.