I sometimes need to temporarily write something in somewhere. I make a 'Temp' google doc, every times I use it I need to remove the content previously there. I want to make a google apps script to add a button to remove all content and format in doc. I try the following code:

function onOpen() {
    .addItem("Clear", "dialogs")

function dialogs() {
  var ui = DocumentApp.getUi();
  var result = ui.alert(
    "Please Confirm",
    "Are you sure you want to clear the document?",
  if (result == ui.Button.YES) {DocumentApp.getActiveDocument().getBody().clear()}

However it doesn't work. How to fix that?

  • 1
    If you are adding the content to the document body use Body.clear(). If you need further help, please start by reading developers.google.com/apps-script/guides/docs, show what you have tried, and add a brief description of your search efforts as is suggested in How to Ask.
    – Rubén
    Sep 8, 2022 at 23:20
  • I try it but it said 'Exception: could not remove the last paragraph of the document'. I try a few other ways but they never work. Edit: Now I know why, the problem solve when I add a blank line at the end of the document. Thank you!
    – Flyson
    Sep 9, 2022 at 1:07
  • 1
    I just reopened the question... I suggest you to post the solution as an answer.
    – Rubén
    Sep 9, 2022 at 1:39

1 Answer 1


When using .getBody().clear(), you need to make sure the last line of the document is not in bullet points or other special formats. Add a blank line in the last paragraph, and the code should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.