Document Automation Made Simple

How to delete PDF pages programmatically

Learn how to delete PDF pages programmatically. Remove single pages and page ranges, or detect and drop blank scan pages automatically, with ready-to-run code samples in C#, Python, Node.js, PHP, and Java.

Kostas, Developer

PDFs collect pages you don't want: terms-and-conditions boilerplate in invoices, blank pages from double-sided scanning, cover sheets from fax gateways, or sections that simply don't belong in the final document. Opening each file in an editor does not scale - if your application handles documents, you need to delete PDF pages programmatically.

This guide shows how to do it with the Delete PDF Pages API: removing single pages and page ranges, detecting and dropping blank pages automatically, and ready-to-run code in C#, Python, Node.js, PHP, and Java.

Quick start: delete pages with one API call

Post your file and list the pages to remove:

curl -X POST https://v2.convertapi.com/convert/pdf/to/delete-pages \
 -H "Authorization: Bearer api_token" \
 -F "File=@/path/to/document.pdf" \
 -F "PageRange=1,5,7-10"

The response contains the PDF with those pages gone. No PDF libraries to install, no page-tree internals to learn.

Page range syntax

The PageRange parameter accepts single pages, ranges, and any combination of both, separated by commas:

  • 5 - delete one page
  • 1-10 - delete a range
  • 1,5,7-10,13-15 - mix individual pages and ranges in one call

Page numbers are 1-based, and documents with up to 20,000 pages are supported, so batch-processing large archives is not a problem.

Remove blank pages from a PDF automatically

Blank pages usually sneak in through double-sided scanning (every one-sided original leaves an empty back side) or from print-to-PDF drivers padding documents. Hunting their page numbers manually defeats the point of automation, so the API can detect and remove them for you:

curl -X POST https://v2.convertapi.com/convert/pdf/to/delete-pages \
 -H "Authorization: Bearer api_token" \
 -F "File=@/path/to/scanned-document.pdf" \
 -F "DeleteBlankPages=true"

Set DeleteBlankPages to true and every blank page is dropped automatically - no page numbers needed. It also works alongside PageRange, so you can strip a cover sheet and clean up blank scan pages in a single call.

Delete PDF pages in C#

Install the ConvertAPI .NET SDK from NuGet:

dotnet add package ConvertApi
using ConvertApiDotNet;

var convertApi = new ConvertApi("api_token");

var result = await convertApi.ConvertAsync("pdf", "delete-pages",
    new ConvertApiFileParam(@"C:\docs\report.pdf"),
    new ConvertApiParam("PageRange", "1,5,7-10")
);

await result.SaveFilesAsync(@"C:\docs\trimmed");

Delete PDF pages in Python

pip install convertapi
import convertapi

convertapi.api_credentials = 'api_token'

convertapi.convert('delete-pages', {
    'File': '/path/to/scanned-document.pdf',
    'DeleteBlankPages': True
}, from_format = 'pdf').save_files('/path/to/cleaned')

Delete PDF pages in Node.js

npm install convertapi
const convertapi = require('convertapi')('api_token');

convertapi.convert('delete-pages', {
    File: './files/report.pdf',
    PageRange: '2,5-8'
}, 'pdf').then(function(result) {
    return result.saveFiles('./files/trimmed');
});

Delete PDF pages in PHP

composer require convertapi/convertapi-php
ConvertApi::setApiCredentials('api_token');

$result = ConvertApi::convert('delete-pages', [
        'File' => '/path/to/scanned-document.pdf',
        'DeleteBlankPages' => 'true',
    ], 'pdf'
);

$result->saveFiles('/path/to/cleaned');

Delete PDF pages in Java

<dependency>
    <groupId>com.convertapi.client</groupId>
    <artifactId>convertapi</artifactId>
    <version>2.10</version>
</dependency>
Config.setDefaultApiCredentials("api_token");

ConvertApi.convert("pdf", "delete-pages",
    new Param("File", Paths.get("/path/to/report.pdf")),
    new Param("PageRange", "1,5,7-10")
).get().saveFilesSync(Paths.get("/path/to/trimmed"));

Chain it with other PDF tools

Deleting pages is often one step in a longer pipeline. The same API family covers the rest, so you can chain actions without downloading intermediate files:

The full toolbox lives on the PDF Tools REST API page.

Frequently asked questions

How do I remove blank pages from a PDF automatically?

Pass DeleteBlankPages=true to the delete-pages endpoint. The API detects blank pages - typical leftovers from double-sided scanning - and removes them without you specifying any page numbers.

Can I delete several page ranges in one call?

Yes. PageRange takes any comma-separated combination of pages and ranges, like 1,5,7-10,13-15, so one request handles even complex cleanups.

Can I delete pages from a password-protected PDF?

Yes. Pass the document password in the Password parameter and the API will open the file, delete the pages, and return the result.

Does deleting pages reduce the PDF file size?

Yes - removing pages removes their content. If size is the main goal, pair it with the compress endpoint; see our guide on compressing PDF files programmatically.

Conclusion

Deleting PDF pages programmatically is a single HTTP call: list the pages in PageRange, or let DeleteBlankPages clean up scans automatically. Try it in the browser on the Delete PDF Pages API page - the interactive demo generates a working snippet for your language while you tweak the parameters.


Related converters

Ready to Streamline Your File Conversions?