Skip to content

Commit 7b4891f

Browse files
committed
Merge branch 'fix/mon-clean-volumes' into 'main'
fix(cli): mon clean now properly removes containers and volumes See merge request postgres-ai/postgres_ai!144
2 parents a007650 + e41a740 commit 7b4891f

1 file changed

Lines changed: 51 additions & 17 deletions

File tree

cli/bin/postgres-ai.ts

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,34 +1765,68 @@ mon
17651765
});
17661766
mon
17671767
.command("clean")
1768-
.description("cleanup monitoring services artifacts")
1769-
.action(async () => {
1770-
console.log("Cleaning up Docker resources...\n");
1768+
.description("cleanup monitoring services artifacts (stops services and removes volumes)")
1769+
.option("--keep-volumes", "keep data volumes (only stop and remove containers)")
1770+
.action(async (options: { keepVolumes?: boolean }) => {
1771+
console.log("Cleaning up monitoring services...\n");
17711772

17721773
try {
1773-
// Remove stopped containers
1774-
const { stdout: containers } = await execFilePromise("docker", ["ps", "-aq", "--filter", "status=exited"]);
1775-
if (containers.trim()) {
1776-
const containerIds = containers.trim().split('\n');
1777-
await execFilePromise("docker", ["rm", ...containerIds]);
1778-
console.log("✓ Removed stopped containers");
1774+
// First, use docker-compose down to properly stop and remove containers/volumes
1775+
const downArgs = options.keepVolumes ? ["down"] : ["down", "-v"];
1776+
console.log(options.keepVolumes
1777+
? "Stopping and removing containers (keeping volumes)..."
1778+
: "Stopping and removing containers and volumes...");
1779+
1780+
const downCode = await runCompose(downArgs);
1781+
if (downCode === 0) {
1782+
console.log("✓ Monitoring services stopped and removed");
17791783
} else {
1780-
console.log("✓ No stopped containers to remove");
1784+
console.log("⚠ Could not stop services (may not be running)");
1785+
}
1786+
1787+
// Remove orphaned volumes from previous installs with different project names
1788+
if (!options.keepVolumes) {
1789+
const volumePatterns = [
1790+
"monitoring_grafana_data",
1791+
"monitoring_postgres_ai_configs",
1792+
"monitoring_sink_postgres_data",
1793+
"monitoring_target_db_data",
1794+
"monitoring_victoria_metrics_data",
1795+
"postgres_ai_configs_grafana_data",
1796+
"postgres_ai_configs_sink_postgres_data",
1797+
"postgres_ai_configs_target_db_data",
1798+
"postgres_ai_configs_victoria_metrics_data",
1799+
"postgres_ai_configs_postgres_ai_configs",
1800+
];
1801+
1802+
const { stdout: existingVolumes } = await execFilePromise("docker", ["volume", "ls", "-q"]);
1803+
const volumeList = existingVolumes.trim().split('\n').filter(Boolean);
1804+
const orphanedVolumes = volumeList.filter(v => volumePatterns.includes(v));
1805+
1806+
if (orphanedVolumes.length > 0) {
1807+
let removedCount = 0;
1808+
for (const vol of orphanedVolumes) {
1809+
try {
1810+
await execFilePromise("docker", ["volume", "rm", vol]);
1811+
removedCount++;
1812+
} catch {
1813+
// Volume might be in use, skip silently
1814+
}
1815+
}
1816+
if (removedCount > 0) {
1817+
console.log(`✓ Removed ${removedCount} orphaned volume(s) from previous installs`);
1818+
}
1819+
}
17811820
}
17821821

1783-
// Remove unused volumes
1784-
await execFilePromise("docker", ["volume", "prune", "-f"]);
1785-
console.log("✓ Removed unused volumes");
1786-
1787-
// Remove unused networks
1822+
// Remove any dangling resources
17881823
await execFilePromise("docker", ["network", "prune", "-f"]);
17891824
console.log("✓ Removed unused networks");
17901825

1791-
// Remove dangling images
17921826
await execFilePromise("docker", ["image", "prune", "-f"]);
17931827
console.log("✓ Removed dangling images");
17941828

1795-
console.log("\nCleanup completed");
1829+
console.log("\n✓ Cleanup completed - ready for fresh install");
17961830
} catch (error) {
17971831
const message = error instanceof Error ? error.message : String(error);
17981832
console.error(`Error during cleanup: ${message}`);

0 commit comments

Comments
 (0)