How to Delete Every 5th and 6th Page Using Acrobat JavaScript
Question details
The user needs to automate deleting the 5th and 6th pages of every 6-page group in a 149-group PDF document.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Automating repetitive page deletion tasks in a large PDF document to remove specific repeating pages.
- Observed behavior
- The user wants to implement an Acrobat JavaScript snippet via the Action Wizard that accurately removes the targeted pages without disrupting the document's page indexing.
Always duplicate your PDF file and work on the copy before executing JavaScript deletions, as script actions generally cannot be undone.
Use a Reverse-Loop JavaScript Snippet
Execute a reverse-iteration loop in Acrobat to securely delete specific pages while keeping preceding page indexes intact.
When deleting pages programmatically in Acrobat, you must start from the end of the document and move backward. If you delete from the beginning, the index numbers of all subsequent pages will shift backward, causing the script to miscalculate and delete the wrong pages.
In Adobe Acrobat Pro, open the Action Wizard from the Tools menu to apply a script to the document, or simply press Ctrl+J (Windows) or Cmd+J (Mac) to open the JavaScript Debugger.
Type or paste the following code into the console: for (var p = this.numPages - 1; p > 0; p -= 6) { this.deletePages(p - 1, p); }
Run the script. This loop iterates backwards in intervals of 6, targeting the 5th and 6th pages of each group. Because Acrobat page indexes start at 0, it deletes indexes 'p-1' and 'p' securely.

Manage and Organize PDFs Easily with WPS Office
While advanced JavaScript execution is unique to Acrobat, WPS Office offers a highly intuitive visual interface for deleting, extracting, and reordering PDF pages, making standard document management easily accessible without needing to write custom code.

Frequently Asked Questions
Why do we iterate backward when deleting pages with JavaScript?
Iterating backward ensures that when a page is deleted, the index numbers of the remaining pages that appear earlier in the document do not change. This prevents the script from accidentally shifting indices and deleting the wrong pages.
Can I use this script for different page group sizes?
Yes. To modify the script for a different group size, change the loop interval 'p -= 6' to match your new total group size, and adjust the 'this.deletePages()' parameters to target the correct zero-indexed pages within that specific group.
Can I undo page deletions made via Acrobat JavaScript?
No, scripts executed via the JavaScript Debugger or Action Wizard typically cannot be undone using the standard 'Undo' command. You should always run destructive scripts on a backup copy of your PDF.




