-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
🚮 feat: Enhance "Delete User" Script #7899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danny-avila
merged 5 commits into
dev
from
7898-fix-delete-user-script-allow-deep-delete
Jun 15, 2025
+94
−28
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
691f885
🔧 fix: Enhance user deletion script to allow deep deletion of related…
rubentalstra 92aeab4
🔧 fix: Update user deletion script to confirm deep deletion of transa…
rubentalstra f097c08
🔧 fix: Refactor user deletion script to use graceful exit and ensure …
rubentalstra 702d9fa
Update config/delete-user.js
rubentalstra f2581fe
Merge branch 'dev' into 7898-fix-delete-user-script-allow-deep-delete
rubentalstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,116 @@ | ||
#!/usr/bin/env node | ||
const path = require('path'); | ||
const mongoose = require(path.resolve(__dirname, '..', 'api', 'node_modules', 'mongoose')); | ||
const { User } = require('@librechat/data-schemas').createModels(mongoose); | ||
const { | ||
User, | ||
Agent, | ||
Assistant, | ||
Balance, | ||
Transaction, | ||
ConversationTag, | ||
Conversation, | ||
Message, | ||
File, | ||
Key, | ||
MemoryEntry, | ||
PluginAuth, | ||
Prompt, | ||
PromptGroup, | ||
Preset, | ||
Session, | ||
SharedLink, | ||
ToolCall, | ||
Token, | ||
} = require('@librechat/data-schemas').createModels(mongoose); | ||
require('module-alias')({ base: path.resolve(__dirname, '..', 'api') }); | ||
const { askQuestion, silentExit } = require('./helpers'); | ||
const connect = require('./connect'); | ||
|
||
async function gracefulExit(code = 0) { | ||
try { | ||
await mongoose.disconnect(); | ||
} catch (err) { | ||
console.error('Error disconnecting from MongoDB:', err); | ||
} | ||
silentExit(code); | ||
} | ||
|
||
(async () => { | ||
await connect(); | ||
|
||
/** | ||
* Show the welcome / help menu | ||
*/ | ||
console.purple('---------------'); | ||
console.purple('Deleting a user'); | ||
console.purple('Deleting a user and all related data'); | ||
console.purple('---------------'); | ||
|
||
let email = ''; | ||
if (process.argv.length >= 3) { | ||
email = process.argv[2]; | ||
} else { | ||
// 1) Get email | ||
let email = process.argv[2]; | ||
if (!email) { | ||
email = await askQuestion('Email:'); | ||
} | ||
let user = await User.findOne({ email: email }); | ||
if (user !== null) { | ||
if ((await askQuestion(`Delete user ${user}?`)) === 'y') { | ||
user = await User.findOneAndDelete({ _id: user._id }); | ||
if (user !== null) { | ||
console.yellow(`Deleted user ${user}`); | ||
} else { | ||
console.yellow(`Couldn't delete user with email ${email}`); | ||
} | ||
} | ||
} else { | ||
console.yellow(`Didn't find user with email ${email}`); | ||
|
||
// 2) Find user | ||
const user = await User.findOne({ email: email.toLowerCase() }); | ||
if (!user) { | ||
console.yellow(`No user found with email "${email}"`); | ||
return gracefulExit(0); | ||
} | ||
|
||
// 3) Confirm full deletion | ||
const confirmAll = await askQuestion( | ||
`Really delete user ${user.email} (${user._id}) and ALL their data? (y/N)`, | ||
); | ||
if (confirmAll.toLowerCase() !== 'y') { | ||
console.yellow('Aborted.'); | ||
return gracefulExit(0); | ||
} | ||
|
||
silentExit(0); | ||
})(); | ||
// 4) Ask specifically about transactions | ||
const confirmTx = await askQuestion('Also delete all transaction history for this user? (y/N)'); | ||
const deleteTx = confirmTx.toLowerCase() === 'y'; | ||
|
||
process.on('uncaughtException', (err) => { | ||
if (!err.message.includes('fetch failed')) { | ||
console.error('There was an uncaught error:'); | ||
console.error(err); | ||
const uid = user._id.toString(); | ||
|
||
// 5) Build and run deletion tasks | ||
const tasks = [ | ||
Agent.deleteMany({ author: uid }), | ||
Assistant.deleteMany({ user: uid }), | ||
Balance.deleteMany({ user: uid }), | ||
ConversationTag.deleteMany({ user: uid }), | ||
Conversation.deleteMany({ user: uid }), | ||
Message.deleteMany({ user: uid }), | ||
File.deleteMany({ user: uid }), | ||
Key.deleteMany({ userId: uid }), | ||
MemoryEntry.deleteMany({ userId: uid }), | ||
PluginAuth.deleteMany({ userId: uid }), | ||
Prompt.deleteMany({ author: uid }), | ||
PromptGroup.deleteMany({ author: uid }), | ||
Preset.deleteMany({ user: uid }), | ||
Session.deleteMany({ user: uid }), | ||
SharedLink.deleteMany({ user: uid }), | ||
ToolCall.deleteMany({ user: uid }), | ||
Token.deleteMany({ userId: uid }), | ||
]; | ||
|
||
rubentalstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (deleteTx) { | ||
tasks.push(Transaction.deleteMany({ user: uid })); | ||
} | ||
|
||
await Promise.all(tasks); | ||
rubentalstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 6) Finally delete the user document itself | ||
await User.deleteOne({ _id: uid }); | ||
|
||
console.green(`✔ Successfully deleted user ${email} and all associated data.`); | ||
if (!deleteTx) { | ||
console.yellow('⚠️ Transaction history was retained.'); | ||
} | ||
|
||
return gracefulExit(0); | ||
})().catch(async (err) => { | ||
if (!err.message.includes('fetch failed')) { | ||
console.error('There was an uncaught error:'); | ||
console.error(err); | ||
await mongoose.disconnect(); | ||
process.exit(1); | ||
rubentalstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.