All posts
Delete PDF pages via CLI
Ghostscript keeps the pages you list: deleting a page means listing everything else.
Open online converterPackages to install
Use ghostscript. Put the pages you want to keep in -sPageList.
ghostscript
sudo apt-get update
sudo apt-get install -y ghostscriptHow to run the command
To drop page 2 from a 10-page file, keep 1,3-10: gs ... -sPageList=1,3-10 -sOutputFile=cleaned.pdf input.pdf. The range 2- means “from page 2 to the end”.
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=1,3-10 -sOutputFile=cleaned.pdf input.pdfDockerfile example
Build a minimal Ubuntu image with the required packages and run the conversion inside the container.
FROM ubuntu:24.04
RUN apt-get update && \
apt-get install -y --no-install-recommends \
ghostscript && \
rm -rf /var/lib/apt/lists/*
WORKDIR /work
docker build -t delete-pages-pdf .
docker run --rm -v "$PWD:/work" -w /work delete-pages-pdf \
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=1,3-10 -sOutputFile=cleaned.pdf input.pdfExample commands
Run these locally after installing the packages, or inside the container from the Dockerfile.
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=1,3-10 -sOutputFile=cleaned.pdf input.pdfgs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=2- -sOutputFile=without-first.pdf input.pdf