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.
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.
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.
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.
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.
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);
}
Replace the Document ID: Change 'your-google-doc-id-here' to your actual Google Doc ID (found in the URL of your doc)
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
Optional Security: Uncomment the authentication section and add a secret key to validate incoming requests
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.