Delete google docs content and replace it

I am trying to delete the whole content from a google document and then replace it with new content. In my scenario I check if the document exists (Get content of a document), when the document exists I then want to delete all of the content and replace it with new content. The content of the document could be images, paragraphs, tables, pretty much anything HTML allows. Therefore trying to use the replace pre built modules is pretty much impossible as I dont know what parts of the document could have changed. Therefore it would be easier to just delete all content of the document and then upload all new content. Is this possible, I cant seem to find anything that would do this. I have to keep the Id of the document the same but if there is a way of creating new versions or anything like that it would work.

My issue seems similar to this one. Update Google Docs through Make API Call Module

1 Like

Welcome to the Make community!

Absolutely, it can be done as shown here Can a Google Docs content be deleted/cleared?

To do this, you can try using the Google Docs “Make an API Call” module —

Performs an arbitrary authorized API call.

For more information, see https://www.make.com/en/integrations/google-docs, and https://www.make.com/en/help/app/google-docs in the help centre.

You can also use the Hire a Pro category to request for private 1-to-1 assistance via video call/screenshare/private messaging/etc. This may help you get your issue resolved faster especially if it is urgent or contain sensitive information. It is important to post your request in the Hire a Pro category, as forum members are not allowed to advertise their services in other categories like here (even if it’s free/unpaid). Posting in the Hire a Pro category will allow other members to assist you over other forms of communication.

Alternatively, you can use the private messaging feature to directly reach out to other forum members. To do this, go to your profile, and click on the “New Message” button:

Hope this helps! Let me know if there are any further questions or issues.

— @samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

Hi, I have tried using these methods. In other words using the GET to get the content of the article. Then use the returned ‘Body’ variable as the old text to replace and put the HTML body in the new text. This doesn’t seem to work though.

My suspicion is that the replace text module only works for text and as my document can contain images, tables and pretty much anything that can be done in HTML the module doesnt work as expected. To make this work I think it would require a series of complex processing and modules to work out what has been changed. At which point I think I have decided its probably easier to just do what the solution was in your link above, ie create a new document and move the old document into an archive folder.

No, it is literally possible to achieve this with just that one module I mentioned.

You don’t even need a Get Document Content module.

All you need is a deleteContentRange request.

Okay, so if I used the generic google doc api call with the endpoint.
/v1/documents/{documentId}:batchUpdate

Then in my request body I did
{
“requests”: [
{
“range”: {
“startIndex”: 0,
“endIndex”: 10
}
}
]
}

where I have put 10 for end index that would actually be the length of the document.
That should work? I am fairly sure I tried this and it didn’t work.

What’s the error message?

No error message, it just doesnt do anything to the sheet.

Were you able to solve the issue?
I’m trying to do pretty much the same: completely clear the content of a file and replace it with new text.
I tried using the “Get content of a document” module and mapping its output to the “replace text in a file” module.
It won’t work.
No errors, but also no change in the file.

Unfortunately not, it looks like I used the method you are using where you get the document and use the text content to fill in the ‘old text to be replaced’ parameter.
In the end I had to go with a folder system where I moved the file into an archive folder and just created a new document.

Depending on your requirements you can do it easier with PDFs.

1 Like

Thanks. I’ll have to figure.out something else then.

Let me know what you come up with. Thanks

Here’s what I did:

  • A Google Apps Script in the file that will wipe the file when triggered (webhook)
  • A HTTP request in MAKE, triggering the script
  • Once the doc is wiped, use the Insert a Paragraph to a Document module in MAKE to insert the new text

The script:

function doPost(e) {
  try {
    // Replace with your actual Google Doc ID
    const DOC_ID = 'your-google-doc-id-here';
    console.log('doPost triggered');
    console.log('DOC_ID:', DOC_ID);
    // Optional: Add authentication/validation
    // You can check for a secret key or other authentication method
    // const requestBody = JSON.parse(e.postData.contents);
    // if (requestBody.secret !== 'your-secret-key') {
    //   return ContentService.createTextOutput('Unauthorized').setMimeType(ContentService.MimeType.TEXT);
    // }
    // Open the Google Doc
    console.log('Opening document...');
    const doc = DocumentApp.openById(DOC_ID);
    const body = doc.getBody();
    console.log('Document opened, current content length:', body.getText().length);
    // Clear all content from the document - Alternative method
    const text = body.getText();
    if (text.length > 0) {
      console.log('Using alternative clear method');
      body.replaceText('.*', '');
    } else {
      console.log('Document is already empty');
    }
    // Optional: Add a timestamp or confirmation message
    // body.appendParagraph('Document cleared on: ' + new Date().toString());
    // Save the document
    doc.saveAndClose();
    console.log('Document saved');
    // Return success response
    return ContentService
      .createTextOutput(JSON.stringify({
        'status': 'success',
        'message': 'Document content cleared successfully',
        'timestamp': new Date().toISOString()
      }))
      .setMimeType(ContentService.MimeType.JSON)
      .setStatusCode(200);
  } catch (error) {
    // Return error response
    return ContentService
      .createTextOutput(JSON.stringify({
        'status': 'error',
        'message': error.toString(),
        'timestamp': new Date().toISOString()
      }))
      .setMimeType(ContentService.MimeType.JSON)
      .setStatusCode(500);
  }
}
// Optional: Handle GET requests for testing
function doGet(e) {
  return ContentService
    .createTextOutput('Webhook endpoint is active. Use POST to trigger document clearing.')
    .setMimeType(ContentService.MimeType.TEXT);
}
  1. Replace the Document ID: Change 'your-google-doc-id-here' to your actual Google Doc ID (found in the URL of your doc)
  2. Deploy the script:
  • In Google Apps Script, click “Deploy” > “New deployment”
  • Choose type: “Web app”
  • Execute as: “Me”
  • Who has access: “Anyone” (or “Anyone with Google account” for more security)
  • Click “Deploy” and copy the web app URL
  1. Optional Security: Uncomment the authentication section and add a secret key to validate incoming requests
  2. Permissions: The first time you run this, Google will ask for permissions to access your Google Docs

The script responds with JSON indicating success or failure, making it easy to verify the webhook worked. The document will remain but all its content will be deleted each time the webhook is triggered.

1 Like