PDF Editing Issues

How to Delete Only Empty PDF Comments Using JavaScript

Kushani NimanthikaKushani Nimanthika Nov 10, 2026 868 views

Question details

The user wants to remove only empty annotations (like sticky notes, rectangles, and circles) from a PDF without deleting comments that contain text.

How to Delete Only Empty PDF Comments Using JavaScript
Product
PDF Editor
Device & OS
not provided
Scenario
Cleaning up a PDF document that has numerous annotations, some of which are completely blank, without ruining the existing review notes.
Observed behavior
Standard deletion methods might remove all annotations at once. The goal state is to selectively delete only those missing text contents.
Before you start

Before running any JavaScript on your PDF, make sure to save a backup copy of your document to prevent accidental loss of important annotations.

Solution 1Recommended

Use JavaScript to Delete Empty Annotations

Run a simple JavaScript code snippet to iterate through all annotations and delete only those with empty text content.

You can use the JavaScript console in advanced PDF editors to automate the deletion of blank comments. This method iterates through every annotation in the document and checks its content property before destroying it.

1
Open the JavaScript Console

Open your PDF document in a PDF editor that supports JavaScript execution (like Adobe Acrobat Pro). Locate and open the JavaScript Debugger or Console from the tools menu.

2
Enter the Deletion Script

Type or paste the following script into the console: var annots = this.getAnnots(); if (annots != null) { for (var i = 0; i < annots.length; i++) { if (annots[i].contents == '') annots[i].destroy(); } }

3
Execute the Code

Run the script. The code will scan through all annotations in the document, such as sticky notes or shapes, and destroy only the ones where the text is completely blank.

Use JavaScript to Delete Empty Annotations
Script Explanation: The condition if (annots[i].contents == '') specifically targets and removes annotations without text, safely preserving all your actual written comments.
Visual PDF Management

Manage and Delete PDF Comments Easily with WPS Office

While JavaScript is a powerful way to manage files, WPS Office provides an intuitive Comment Pane that lets you visually review, filter, and delete blank annotations with just a few clicks, requiring no coding skills.

  1. 1. Open PDF in WPS Office: Launch WPS Office and open your PDF document containing the blank annotations.
  2. 2. Access the Comment Pane: Navigate to the 'Comment' tab at the top of the screen and click on the 'Comment Pane' icon to display a list of all document annotations.
  3. 3. Identify Blank Comments: Scroll through the comment list to quickly spot annotations (like rectangles or empty sticky notes) that have no text contents.
  4. 4. Delete Unwanted Annotations: Right-click the empty comments in the pane and select 'Delete' or simply press the Delete key to remove them while preserving your actual text notes.
Visual Comment Pane to easily spot and manage blank annotationsBatch delete comments without writing complex JavaScriptFully compatible with Microsoft Office formats and standard PDF documentsLightweight, fast, and user-friendly interface
microsoft office alternative - wps office

Frequently Asked Questions

Can I delete all PDF comments at once instead of just the empty ones?

Yes, most PDF editors have a built-in option to 'Delete All Comments' in their Comment or Review tabs. In JavaScript, you can simply remove the IF condition and run annots[i].destroy(); directly inside the loop.

Why do empty annotations appear in my PDF in the first place?

Empty annotations often occur when users accidentally click the document with a markup tool selected, such as a sticky note or shape tool, without typing any text afterward. Over time, these can clutter the document.

Does this JavaScript code work in all PDF readers?

No, JavaScript execution for PDFs is primarily supported by advanced editors like Adobe Acrobat Pro. Standard PDF viewers or web browsers may not execute these scripts for security reasons.

Will this script delete my highlighted text if there is no pop-up note attached?

If a highlight annotation does not contain a pop-up text note, its 'contents' property might be evaluated as empty, causing it to be deleted by this script. You may need to add a check for the annotation type (e.g., if (annots[i].type != 'Highlight')) to preserve them.