Delete the Last Two PDF Pages with Acrobat JavaScript
Question details
Delete the last two pages of a PDF using Acrobat JavaScript.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- The user wants a script that removes the final two pages regardless of the document's length.
- Observed behavior
- Acrobat JavaScript uses zero-based page indexes, so the last two pages of a seven-page PDF have indexes 5 and 6.
Open the PDF in an Acrobat edition that supports page deletion and the JavaScript console. Check that the document has more than two pages and that its permissions allow page editing.
Delete the Last Two Pages with a Page-Count Script
Use this script when the PDF may have different page counts.
The script uses numPages to find the final two zero-based indexes. The condition prevents it from running on a document with two or fewer pages.
Open the PDF in Acrobat, then press Ctrl+J on Windows or Command+J on macOS to open the JavaScript console.
In the console, enter `if (this.numPages > 2) this.deletePages(this.numPages - 2, this.numPages - 1);`. Select the line and press Ctrl+Enter on Windows or Command+Enter on macOS to run it.
Check the page thumbnails to confirm the final two pages were removed, then choose File > Save or File > Save As.

Delete the Last Two PDF Pages in WPS Office
WPS PDF can remove the final two pages through its page management tools, without running Acrobat JavaScript.
- 1. Open the PDF: Open WPS Office and use Open to select your PDF.
- 2. Find the last pages: Open the page thumbnail panel and identify the final two pages.
- 3. Delete the pages: Select the last two thumbnails, then use the Delete Pages command in the page tools or thumbnail context menu.
- 4. Save the result: Review the remaining pages and choose Save or Save As.

Frequently Asked Questions
Why does the script use indexes 5 and 6 for a seven-page PDF?
Acrobat JavaScript starts page indexes at 0. In a seven-page PDF, the indexes run from 0 through 6, so `this.deletePages(5, 6);` removes pages 6 and 7.
Will the page-count script work on PDFs of different lengths?
Yes. `this.numPages - 2` and `this.numPages - 1` identify the final two indexes. The supplied condition runs the deletion only when the PDF has more than two pages.
What happens if the PDF has only two pages?
The condition `this.numPages > 2` is false, so the script does not delete any pages.
Why might Acrobat refuse to delete the pages?
Page deletion may be unavailable in your Acrobat edition or restricted by the PDF's permissions. Check the document's security settings and use an edition that supports page editing.




