Delete PDF Pages After Page Two with Acrobat JavaScript
Question details
Configure a reset button to remove all generated PDF pages after the first two pages.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A PDF form contains generated template pages that should be removed when the user clicks a reset button.
- Observed behavior
- The desired result is to keep pages one and two and delete every subsequent page.
Open the PDF in Adobe Acrobat and confirm that it has a button you can edit under Prepare a Form. Check that the document allows page deletion and that Acrobat JavaScript is enabled.
Add a Delete Pages Action to the Acrobat Reset Button
Use a button JavaScript action to delete page three through the last page while keeping the first two.
Acrobat uses zero-based page indexes, so index 2 is the third page. The condition below avoids attempting a deletion when the document has only two pages or fewer.
In Acrobat, open Prepare a Form, select the reset button, and open its Properties.
On the Actions tab, select Mouse Up as the trigger and Run a JavaScript as the action, then click Add.
In the JavaScript editor, enter: if (this.numPages > 2) { this.deletePages(2, this.numPages - 1); }
Confirm the script and button properties, save the PDF, exit form editing, and click the reset button. Verify that only the first two pages remain.

Manage PDF Documents and Office Files with WPS Office
WPS Office is a free, lightweight alternative with a familiar interface and support for common Office formats, making it easy to move between documents. Its PDF tools can help with page management, but they do not replace the Acrobat JavaScript button action described above.

Frequently Asked Questions
Why does the script start deleting at page index 2?
Acrobat numbers pages from zero in JavaScript: index 0 is page one, index 1 is page two, and index 2 is page three.
What happens if the PDF has only two pages?
The condition this.numPages > 2 prevents the deletePages call, so both pages remain unchanged.
Will the button also clear form fields?
No. The deletePages action removes pages only. Add a separate form-reset action in the button's Actions tab if field values also need to be cleared.
Can this button delete pages in Adobe Acrobat Reader?
Page deletion may not be available in Reader. Test the button in an Acrobat environment that permits page deletion, and check the PDF's security restrictions if it does not work.




