Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Remove tags from secrets when tag is deleted

Maidul Islam31293bbe0651
diff --git a/backend/src/controllers/v2/tagController.ts b/backend/src/controllers/v2/tagController.ts
index b737eb41a7..199f2401e9 100644
--- a/backend/src/controllers/v2/tagController.ts
+++ b/backend/src/controllers/v2/tagController.ts
@@ -2,7 +2,7 @@ import { Request, Response } from 'express';
import * as Sentry from '@sentry/node';
import { Types } from 'mongoose';
import {
- Membership,
+ Membership, Secret,
} from '../../models';
import Tag, { ITag } from '../../models/tag';
import { Builder } from "builder-pattern"
@@ -54,6 +54,12 @@ export const deleteWorkspaceTag = async (req: Request, res: Response) => {
const result = await Tag.findByIdAndDelete(tagId);
+ // remove the tag from secrets
+ await Secret.updateMany(
+ { tags: { $in: [tagId] } },
+ { $pull: { tags: tagId } }
+ );
+
res.json(result);
}
diff --git a/backend/src/models/secret.ts b/backend/src/models/secret.ts
index 4ac6c768d0..463ed50fde 100644
--- a/backend/src/models/secret.ts
+++ b/backend/src/models/secret.ts
@@ -109,6 +109,9 @@ const secretSchema = new Schema<ISecret>(
}
);
+
+secretSchema.index({ tags: 1 }, { background: true })
+
const Secret = model<ISecret>('Secret', secretSchema);
export default Secret;