npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

clean-orphan-uploads-cli

v1.0.0

Published

CLI to clean orphaned uploaded files based on PostgreSQL image references

Readme

clean-orphan-uploads-cli

CLI to remove orphan upload files in a local folder based on image path reference data in PostgreSQL.

This tool is safe to use incrementally because it supports --dry-run mode for simulation before files are actually deleted.

How It Works

  1. The CLI reads all image path values from the PostgreSQL table.
  2. The CLI reads all files in the local upload folder.
  3. Files that are no longer referenced by the database are considered orphan.
  4. Orphan files will:
    • only be reported if using --dry-run
    • be permanently deleted without --dry-run

Prerequisites

  • Node.js >=20
  • Accessible PostgreSQL database
  • Data structure containing image paths (default: projects table, image column)

Installation

Option 1: Install as a package

npm install clean-orphan-uploads-cli

Option 2: Run from this source repo

npm install github:udenbaguse/clean-orphan-uploads-cli

Step-by-Step Usage (Recommended)

Step 1 - Identify the upload folder to clean

Example default folder:

public/uploads

If your folder is different, use --uploads-dir <path> later.

Step 2 - Prepare database connection

Choose one of the following methods.

Method A (simplest): use --db-url

clean-orphan-uploads --db-url "postgres://user:pass@localhost:5432/dbname" --dry-run

Method B: use environment variable

If --db-url is not provided, the CLI will read from env

dotenv is automatically loaded, so you can use a .env file.

Example .env:

DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydb
DB_USER=postgres
DB_PASSWORD=secret

Step 3 - Always run a simulation first (--dry-run)

This is a mandatory step before actual deletion:

clean-orphan-uploads --dry-run

Example output:

Dry-run complete. 12 file(s) would be removed from 130 file(s).

Step 4 - Review simulation results

If the number of files to be deleted makes sense, proceed to step 5.

If not as expected, check:

  • --uploads-dir
  • --table
  • --column
  • --path-prefix
  • DB connection configuration

Step 5 - Execute orphan file deletion

Run without --dry-run:

clean-orphan-uploads

Example output:

Removed 12 orphaned file(s) from 130 file(s).

Step 6 - Post-execution verification

  • Check that the upload folder contents are correct.
  • Run --dry-run again to ensure all orphans have been cleaned.

Options

  • --dry-run preview files that would be deleted without actually deleting them
  • --uploads-dir <path> upload folder (default: public/uploads)
  • --db-url <url> PostgreSQL connection string (DATABASE_URL if not set)
  • --table <name> table name (default: projects)
  • --column <name> image path column name (default: image)
  • --path-prefix <prefix> path prefix in DB (default: /uploads/)

Common Command Examples

Default (table projects, column image, folder public/uploads)

clean-orphan-uploads --dry-run

Custom upload folder

clean-orphan-uploads --uploads-dir storage/uploads --dry-run

Custom table and column

clean-orphan-uploads --table products --column thumbnail --dry-run

Custom path prefix

clean-orphan-uploads --path-prefix /media/ --dry-run

Full command with DB URL

clean-orphan-uploads \
  --db-url "postgres://user:pass@localhost:5432/dbname" \
  --uploads-dir public/uploads \
  --table projects \
  --column image \
  --path-prefix /uploads/ \
  --dry-run

Important Notes

  • The CLI only processes files in the first level of the upload folder (not recursive into subfolders).
  • --table and --column values must be valid SQL identifiers.
  • If the upload folder is not found, the process will be skipped with the message:
No uploads directory found at: <path>

Recommended Script Keywords (package.json)

"clean:uploads": "node node_modules/clean-orphan-uploads-cli/bin/clean-orphan-uploads.js",
"clean:uploads:dry": "node node_modules/clean-orphan-uploads-cli/bin/clean-orphan-uploads.js --dry-run"