Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4804e55
feat: create script to enqueue inactive organizations for deletion
wilsonrivera Dec 16, 2025
a0c978a
chore: update script and tests
wilsonrivera Dec 18, 2025
ea9ecda
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Dec 18, 2025
e82d01a
chore: linting and tests
wilsonrivera Dec 18, 2025
67c0a22
chore: remove non-null assertion
wilsonrivera Dec 18, 2025
9b98d21
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Dec 19, 2025
2394618
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Dec 22, 2025
df04df0
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Jan 5, 2026
27dcdf9
chore: implement as organization cleanup as cron job
wilsonrivera Jan 7, 2026
d7cd912
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Jan 7, 2026
b24bb91
chore: fix test
wilsonrivera Jan 7, 2026
e1c4068
Merge remote-tracking branch 'origin/wilson/eng-7753-delete-inactive-…
wilsonrivera Jan 7, 2026
8ac01c0
chore: fix commented `continue`
wilsonrivera Jan 7, 2026
4cf6c72
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Jan 7, 2026
499ec46
chore: update schedule to run once a month
wilsonrivera Jan 7, 2026
d0b68fb
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Jan 8, 2026
9911158
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Jan 16, 2026
cee6641
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Jan 20, 2026
70b005c
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Jan 20, 2026
f709e35
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Jan 23, 2026
0a2bfb8
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Jan 27, 2026
37f0fb7
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Mar 11, 2026
53101a0
chore: minor update
wilsonrivera Mar 11, 2026
c393bf1
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Mar 30, 2026
bca330c
feat: switch from `bullmq` scheduled job to Kubernetes cronjob
wilsonrivera Mar 30, 2026
d35b434
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Mar 30, 2026
e795a34
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Mar 30, 2026
c3cfba2
chore: shorten job name
wilsonrivera Mar 30, 2026
6c2e6bc
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Mar 31, 2026
7439a22
Merge branch 'main' into wilson/eng-7753-delete-inactive-organizations
wilsonrivera Apr 9, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions controlplane/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@wundergraph/protographic": "workspace:*",
"axios": "1.13.5",
"axios-retry": "^4.5.0",
"bullmq": "^5.10.0",
"bullmq": "5.66.4",
"cookie": "^0.7.2",
"date-fns": "^3.6.0",
"dotenv": "^16.4.5",
Expand All @@ -79,7 +79,7 @@
"graphql": "^16.9.0",
"http-proxy-agent": "8.0.0",
"https-proxy-agent": "8.0.0",
"ioredis": "^5.4.1",
"ioredis": "5.8.2",
"isomorphic-dompurify": "^2.33.0",
"jose": "^5.2.4",
"lodash": "^4.17.21",
Expand Down
184 changes: 184 additions & 0 deletions controlplane/src/bin/delete-inactive-orgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import 'dotenv/config';
import process from 'node:process';
import { and, count, eq, gte, isNull, lt, or, sql } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import { pino } from 'pino';
import { addDays, startOfMonth, subDays } from 'date-fns';
import * as schema from '../db/schema.js';
import { buildDatabaseConnectionConfig } from '../core/plugins/database.js';
import { createRedisConnections } from '../core/plugins/redis.js';
import { OrganizationRepository } from '../core/repositories/OrganizationRepository.js';
import { DeleteOrganizationQueue } from '../core/workers/DeleteOrganizationWorker.js';
import { NotifyOrganizationDeletionQueuedQueue } from '../core/workers/NotifyOrganizationDeletionQueuedWorker.js';
import Keycloak from '../core/services/Keycloak.js';
import { getConfig } from './get-config.js';

// The number of days the organization needs to be inactive for before we consider it for deletion
const MIN_INACTIVITY_DAYS = 90;

// How long should we wait before deleting the organization?
const DELAY_FOR_ORG_DELETION_IN_DAYS = 7;

const {
realm,
loginRealm,
apiUrl,
adminUser,
adminPassword,
clientId,
databaseConnectionUrl,
databaseTlsCa,
databaseTlsCert,
databaseTlsKey,
redis,
} = getConfig();

// Create the redis connection.
const { redisQueue, redisWorker } = await createRedisConnections({
host: redis.host!,
port: Number(redis.port),
password: redis.password,
tls: redis.tls,
});

// Create the database connection. TLS is optional.
const connectionConfig = await buildDatabaseConnectionConfig({
tls:
databaseTlsCa || databaseTlsCert || databaseTlsKey
? { ca: databaseTlsCa, cert: databaseTlsCert, key: databaseTlsKey }
: undefined,
});

const queryConnection = postgres(databaseConnectionUrl, { ...connectionConfig });

// Initialize all required services
const logger = pino();
const db = drizzle(queryConnection, { schema: { ...schema } });
const keycloak = new Keycloak({
apiUrl,
realm: loginRealm,
clientId,
adminUser,
adminPassword,
logger: pino(),
});

const orgRepo = new OrganizationRepository(logger, db);
const deleteOrganizationQueue = new DeleteOrganizationQueue(logger, redisQueue);
const notifyOrganizationDeletionQueuedQueue = new NotifyOrganizationDeletionQueuedQueue(logger, redisQueue);

// Do the work!
try {
const now = new Date();
const inactivityThreshold = startOfMonth(subDays(now, MIN_INACTIVITY_DAYS));
const deletesAt = addDays(now, DELAY_FOR_ORG_DELETION_IN_DAYS);

// Retrieve all the organizations that only have a single user
const orgsWithSingleUser = await retrieveOrganizationsWithSingleUser(inactivityThreshold);
if (orgsWithSingleUser.length === 0) {
console.log('No organizations with single user found');
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0);
}

// Process all the organizations with a single user
await keycloak.authenticateClient();
for (const org of orgsWithSingleUser) {
if (!org.userId) {
// Should never be the case but to prevent TypeScript from complaining, we still need to ensure
// that the value exists
continue;
}

// First, we check whether the organization has had any activity registered in the audit logs in the
// last `MIN_INACTIVITY_DAYS` days
const auditLogs = await db
.select({ count: count() })
.from(schema.auditLogs)
.where(and(eq(schema.auditLogs.organizationId, org.id), gte(schema.auditLogs.createdAt, inactivityThreshold)))
.execute();

if (auditLogs.length > 0 && auditLogs[0].count > 0) {
// The organization has had activity registered in the audit, at least once in the last `MIN_INACTIVITY_DAYS` days,
// so we don't need to consider it for deletion
continue;
}

// If the organization hasn't had any activity, we should check the last time the user logged in
try {
const userSessions = await keycloak.client.users.listSessions({
id: org.userId,
realm,
});

const numberOfSessionsRecentlyActive = userSessions.filter(
(sess) => (sess.lastAccess || sess.start) && new Date(sess.lastAccess || sess.start!) >= inactivityThreshold,
).length;

if (numberOfSessionsRecentlyActive > 0) {
// The user has been active at least once in the last `MIN_INACTIVITY_DAYS` days, so we don't need
// to consider it for deletion
continue;
}
} catch (error) {
// Failed to fetch the user sessions, skip for now
console.error(error, `Failed to retrieve sessions for user: ${org.userId}`);
continue;
}

// It seems like the organization (and the user) hasn't been active recently, flag the organization for deletion
console.log(`Queuing organization "${org.slug}" for deletion at ${deletesAt.toISOString()}`);
await queueForDeletion(org.id, now, deletesAt);
}
} catch (err: unknown) {
console.error(err);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
} finally {
redisQueue.disconnect();
redisWorker.disconnect();

await queryConnection.end({ timeout: 1 });
}

async function queueForDeletion(orgId: string, queuedAt: Date, deletesAt: Date) {
// Enqueue the organization deletion job
await orgRepo.queueOrganizationDeletion({
organizationId: orgId,
queuedBy: undefined,
deleteOrganizationQueue,
deleteDelayInDays: DELAY_FOR_ORG_DELETION_IN_DAYS,
});

// Queue the organization deletion notification job
await notifyOrganizationDeletionQueuedQueue.addJob({
organizationId: orgId,
queuedAt: Number(queuedAt),
deletesAt: Number(deletesAt),
});
}

function retrieveOrganizationsWithSingleUser(createdBefore: Date) {
return db
.select({
id: schema.organizations.id,
slug: schema.organizations.slug,
userId: schema.organizations.createdBy,
plan: schema.organizationBilling.plan,
})
.from(schema.organizations)
.innerJoin(schema.organizationsMembers, eq(schema.organizationsMembers.organizationId, schema.organizations.id))
.leftJoin(schema.organizationBilling, eq(schema.organizationBilling.organizationId, schema.organizations.id))
.where(
and(
isNull(schema.organizations.queuedForDeletionAt),
eq(schema.organizations.isDeactivated, false),
lt(schema.organizations.createdAt, createdBefore),
or(isNull(schema.organizationBilling.plan), eq(schema.organizationBilling.plan, 'developer')),
),
)
.groupBy(schema.organizations.id, schema.organizationBilling.plan)
.having(sql`COUNT(${schema.organizationsMembers.id}) = 1`)
.execute();
}
Loading
Loading