Home / Alt manpages / pg_dropcluster(1)

  • pg_dropcluster(1)
  • User command
  • linux

Safely Remove a PostgreSQL Cluster with pg_dropcluster

You will finish with a verified way to remove one Debian-style PostgreSQL cluster, including its data, WAL, tablespaces, logs and generated configuration. This is an irreversible deletion workflow. The examples use pg_dropcluster from postgresql-common version 257build1.1, installed on this system.

Allow about fifteen minutes for the checks, plus however long your backup takes. You need shell access, a recent tested backup if the data matters, and a maintenance window if the cluster is serving clients. The removal command needs elevated privileges. The inspection commands do not.

1. Treat the command as permanent

pg_dropcluster does not merely unregister a cluster. The installed manpage says it removes the data, WAL and tablespace directories, the log file, and configuration files created by pg_createcluster. It may also remove an empty configuration directory under /etc/postgresql/<version>/<cluster> and an empty socket directory, except for /var/run/postgresql and /tmp.

There is no undo option. If you need the cluster again, recovery means restoring a suitable backup or creating a new cluster and restoring its data. A package reinstall will not recover deleted database files.

Destructive action

Do not continue until you have identified the exact cluster and confirmed that its data can be discarded. If this is a production or shared host, record the change and notify users before stopping anything.

2. List the clusters without changing them

Run the Debian cluster listing command as your ordinary account:

$ pg_lsclusters
Ver Cluster Port Status Owner    Data directory              Log file
16  main    5432 online postgres /var/lib/postgresql/16/main /var/log/postgresql/postgresql-16-main.log

Your rows will differ. The two positional values needed by pg_dropcluster are the Ver and Cluster columns. In this example they are 16 and main. Do not use the port, data directory or log file as an argument.

Checkpoint: write down the exact pair you intend to remove, then compare it with the service, application configuration and backup record. For a second look at the target's paths, use the output above. This command only reports state.

3. Confirm the backup and maintenance boundary

Make sure the backup covers the cluster you are about to delete, not just another database on the same host. A logical dump should have completed successfully and be readable enough to test, while a physical backup must be usable for your recovery method. Keep the backup outside the directories that the deletion will remove.

Check which clients or services depend on the cluster before proceeding. Stopping the PostgreSQL server interrupts connections, and deleting the cluster prevents those clients reconnecting to it. If you have no tested recovery path, stop here and create one first.

4. Remove a stopped cluster

If the target is already stopped and your maintenance checks are complete, run the command with sudo:

$ sudo pg_dropcluster 16 main

Replace 16 and main with the pair you confirmed. The command has no separate confirmation prompt in its documented interface, so the command line itself is the final safety boundary. Read it once more before pressing Enter.

A successful run normally returns to the shell without a success message. Verify the cluster list:

$ pg_lsclusters
Ver Cluster Port Status Owner    Data directory              Log file

The removed row should no longer appear. If it still appears, do not repeat the deletion blindly. Read the error, check the version and cluster name, and inspect whether the server is running.

5. Handle a running cluster deliberately

A cluster with a running server attached is normally not deleted. The --stop option overrides that protection by forcing a server shutdown before removing the files. Use it only when stopping this exact cluster is authorised:

$ sudo pg_dropcluster --stop 16 main

This combines service disruption with permanent deletion. It is appropriate for a decommissioned instance or a planned replacement, not as a first response to a connection problem. If you only need to stop a cluster and keep its data, use the appropriate cluster control procedure instead and do not use pg_dropcluster.

Checkpoint after the command: run pg_lsclusters again and confirm that the target row is absent. Also check the service or application that used it. A missing row proves that Debian's cluster metadata no longer lists it; it does not prove that every external client has been reconfigured.

6. Diagnose a refusal without escalating blindly

First check the arguments against pg_lsclusters. The command accepts exactly one optional flag, --stop, followed by a cluster version and cluster name. It does not accept a database name, port or filesystem path in their place.

If the cluster is online and you did not intend to stop it, omit deletion and investigate the service state. If the target name is wrong, correct the pair rather than trying several guesses with sudo. Elevated privileges can authorise a deletion, but they cannot make an incorrect cluster identity safe.

If the command reports missing files or an unexpected state, preserve the error and inspect the host before changing anything else. Do not manually remove remaining PostgreSQL directories to make the listing look clean. Manual cleanup can destroy evidence and can affect a different cluster.

7. Recover if the wrong cluster was removed

There is no rollback command for pg_dropcluster. Stop any automation that might recreate or repopulate the host, preserve relevant logs, and use the recovery plan for the backup you verified earlier. A common path is to create a new cluster with the required version and name, then restore a logical backup. A physical backup has different restore requirements and must be handled according to the backup tool's procedure.

Do not point a new cluster at a partially surviving data directory unless your PostgreSQL recovery procedure explicitly supports that operation. A directory that exists is not evidence of a consistent database.

Done means

  • You matched the exact version and cluster name from pg_lsclusters.
  • You confirmed a tested backup and a recovery path before deletion.
  • You used elevated privileges only for the destructive command.
  • You used --stop only when stopping the target was authorised.
  • The target row is absent from the post-change pg_lsclusters output.
  • You know that recovery requires a backup or a new cluster and restore, not an undo flag.