How to remove pages from a PDF programmatically
Deleting a cover sheet, blank scan, or internal appendix should not require rebuilding the rest of a PDF. Send the source document and the page numbers to discard, then save the cleaned PDF returned by the API.
Remove pages without changing the rest
POST /v1/remove-pages accepts one PDF and a required pages field. The API deletes the named pages and keeps every remaining page in its original order. The output is returned as application/pdf.
Page numbers are 1-indexed. A value of 2,4-6 removes page 2 and pages 4 through 6. The range endpoints are inclusive.
Set up authentication
Create a key at app.filealloy.com and pass it through the X-API-Key header. Treat it like any other server credential: keep it in an environment variable and do not expose it in browser code.
Remove PDF pages with curl
This example removes four pages from packet.pdf and writes the remaining document to a new file:
curl -X POST https://api.filealloy.com/v1/remove-pages \
-H "X-API-Key: ${FILEALLOY_API_KEY}" \
-F "[email protected]" \
-F "pages=2,4-6" \
-o cleaned-packet.pdf Using a different output filename matters. It preserves the source for review and prevents a failed job from leaving you without the original document.
Remove PDF pages in Python
Python's requests package separates file uploads from ordinary form fields. The error branch tries to read FileAlloy's JSON envelope before falling back to plain response text.
from pathlib import Path
import requests
source = Path("packet.pdf")
with source.open("rb") as pdf:
response = requests.post(
"https://api.filealloy.com/v1/remove-pages",
headers={"X-API-Key": "fa_live_your_key"},
files={"file": (source.name, pdf, "application/pdf")},
data={"pages": "2,4-6"},
timeout=60,
)
if not response.ok:
try:
detail = response.json()["error"]["message"]
except (ValueError, KeyError, TypeError):
detail = response.text or "Unknown API error"
raise RuntimeError(f"Remove pages failed ({response.status_code}): {detail}")
Path("cleaned-packet.pdf").write_bytes(response.content)
print("Saved cleaned-packet.pdf") Remove PDF pages in Node.js
Node.js 18 and newer can build the request with its built-in web APIs. Let fetch generate the multipart Content-Type and boundary for you.
import { readFile, writeFile } from "node:fs/promises";
const input = await readFile("packet.pdf");
const form = new FormData();
form.append("file", new Blob([input], { type: "application/pdf" }), "packet.pdf");
form.append("pages", "2,4-6");
const response = await fetch("https://api.filealloy.com/v1/remove-pages", {
method: "POST",
headers: { "X-API-Key": "fa_live_your_key" },
body: form,
});
if (!response.ok) {
const body = await response.json().catch(() => null);
const message = body?.error?.message ?? response.statusText;
throw new Error(`Remove pages failed (${response.status}): ${message}`);
}
const pdf = Buffer.from(await response.arrayBuffer());
await writeFile("cleaned-packet.pdf", pdf);
console.log(`Saved cleaned-packet.pdf (${pdf.length} bytes)`); Build a safe deletion flow
Page deletion is easy to automate and hard to notice after the wrong selection. Add a few guardrails around the API call:
- Show users the 1-indexed page numbers used by the PDF viewer and the request.
- Store the deletion spec with the job record so the result can be traced.
- Keep the original until the output has passed your normal validation or review step.
- If you know the page count, prevent a request that would remove every page.
- Write to a temporary path first, then move the successful output into place.
Handle invalid input and capacity errors
A missing or malformed pages field can return 400. The API uses 413 for an upload over the size limit, 415 for a file that is not a valid PDF, and 422 when the operation fails for that document. A 503 response means the service is at capacity and includes a Retry-After header. A key that has exhausted its monthly quota receives 429.
Do not assume every response contains a PDF. Check the HTTP status first, as the examples do, then write the body only after success.
When extraction is clearer
Removal works best when the unwanted set is short. If a 100-page source should yield only pages 7 through 10, call POST /v1/split with pages=7-10 instead of constructing a long deletion list. Naming what the application wants to keep usually makes the job easier to review.
Clean up a test document
Try the endpoint on a copy of a PDF with obvious page labels, then confirm both the deletion and the order of what remains. API keys are available from app.filealloy.com.