# Build an ONLYOFFICE macro that deletes specific comments in the document

**URL:** https://community.onlyoffice.com/t/build-an-onlyoffice-macro-that-deletes-specific-comments-in-the-document/7586
**Category:** News
**Created:** [17 January 2024 09:03 UTC](https://community.onlyoffice.com/t/build-an-onlyoffice-macro-that-deletes-specific-comments-in-the-document/7586 "2024-01-17T09:03:35Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ONLYOFFICE\_team](https://community.onlyoffice.com/user_avatar/community.onlyoffice.com/onlyoffice_team/32/431_2.png) [@ONLYOFFICE\_team](https://community.onlyoffice.com/u/ONLYOFFICE_team)
#### Post date: [17 January 2024 09:03 UTC](https://community.onlyoffice.com/t/build-an-onlyoffice-macro-that-deletes-specific-comments-in-the-document/7586/1 "2024-01-17T09:03:35Z")

</div>

Originally published at: [Build an ONLYOFFICE macro that deletes specific comments in the document | ONLYOFFICE Blog](https://www.onlyoffice.com/blog/2024/01/build-an-onlyoffice-macro-that-deletes-specific-comments-in-the-document/)

Sometimes, you just need to get rid of comments in your document. In this blog post, we’ll show you how to create a simple macro for ONLYOFFICE that cleans specific or all comments, keeping your collaboration focused and clutter-free.

![Building an ONLYOFFICE macro to tally word occurences in your document](https://static-blog.onlyoffice.com/wp-content/uploads/2023/12/16110939/Delete-specific-or-all-comments-in-text-editor.png)

## Building the macro

```
var oDocument = Api.GetDocument();
```

Starting off, we get hold of the current working document in the _oDocument_ variable.

```
var aComments = oDocument.GetAllComments();
var enteredText = "THE COMMENT YOU WANT TO DELETE";
```

Next, we get all the comments in the macro using the _GetAllComments__()_ method, and set the comment we want to search for. If you want to delete all the comments in the document, you can skip this step and follow the steps below.

```
for (let i = 0; i < aComments.length; i++) {
    var sQuoteText = aComments[i].GetText();
```

Next, we set up the _for loop_ which iterates through the _aComments_ array and gets the comment text for each iteration in _sQuoteText._

```
//aComments[i].Delete(); Uncomment if you want to delete every comment in the document
```

If you want to delete all the comments in the document, you can uncomment this line and run the macro.

```
if (sQuoteText === enteredText) {
      aComments[i].Delete();
    }
  }
```

At last, we check if the entered word (_enteredText_) matches the text in the comment. If it does, we delete the comment on that word with the _delete()_ method.

## The entire code for the macro

Here is the code for the entire macro.

```
(function () {
  //get the active sheets and comments
  var oDocument = Api.GetDocument();
  var aComments = oDocument.GetAllComments();

var enteredText = “THE COMMENT YOU WANT TO DELETE”; //place your comment here

//for loop to iterate through all the comments  
for (let i = 0; i \< aComments.length; i++) {  
var sQuoteText = aComments[i].GetText();  
//aComments[i].Delete(); Uncomment if you want to delete every comment in the document

```
//this if statement deletes all instances of the specified comment from the doc
if (sQuoteText === enteredText) {
  aComments[i].Delete();
}

```

}  
})();
```

Now let’s run our macro and see it in action!

_Deleting all the comments:_

https://www.youtube.com/embed/sd6_WNzd6OM?si=erxHvkS1QTqnL7Gm

_Deleting specific comments:_

https://www.youtube.com/embed/tDHIJasMXLg?si=AN26OfLqxU-rn0Nf

And that wraps up our easy macro tutorial. We’re pretty sure this little tool will make your document editing smoother, saving you time and boosting collaboration. Happy editing!

Don’t miss the chance to harness the power of the ONLYOFFICE API. Our extensive library of API methods is your key to transforming your ideas into reality. If you have any questions or innovative concepts, we encourage you to [share them with us](https://www.onlyoffice.com/contacts.aspx?utm_source=dev_blog&utm_medium=article+&utm_id=delete_comments_macro). Your input is highly valued, and we are excited about the possibility of collaborating with you. Best of luck in your exploratory endeavors!

### Useful links

[An ONLYOFFICE macro that adds comments to specific words](https://www.onlyoffice.com/blog/2024/01/how-to-add-comments-to-every-instance-of-a-word-with-onlyoffice-macros?utm_source=dev_blog&utm_medium=article&utm_id=delete_comments_macro)

[Macro samples](https://api.onlyoffice.com/plugin/macrosamples/?utm_source=dev_blog&utm_medium=article+&utm_id=delete_comments_macro)

[ONLYOFFICE on GitHub](https://github.com/ONLYOFFICE)

[ONLYOFFICE Text Document API](https://api.onlyoffice.com/docbuilder/textdocumentapi?utm_source=dev_blog&utm_medium=article+&utm_id=delete_comments_macro)

[Other macros](https://www.onlyoffice.com/blog/search?s=macro&utm_source=dev_blog&utm_medium=article+&utm_id=delete_comments_macro)
