UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

71 lines 2.86 kB
import { z } from 'zod'; import { cli } from '../../../../cli/cli.js'; import { globalOptionsZod } from '../../../../Command.js'; import request from '../../../../request.js'; import { odata } from '../../../../utils/odata.js'; import GraphCommand from '../../../base/GraphCommand.js'; import commands from '../../commands.js'; export const options = z.strictObject({ ...globalOptionsZod.shape, force: z.boolean().optional().alias('f') }); class EntraUserRecycleBinItemClearCommand extends GraphCommand { get name() { return commands.USER_RECYCLEBINITEM_CLEAR; } get description() { return 'Removes all users from the tenant recycle bin'; } get schema() { return options; } async commandAction(logger, args) { const clearRecycleBinUsers = async () => { try { const users = await odata.getAllItems(`${this.resource}/v1.0/directory/deletedItems/microsoft.graph.user?$select=id`); if (this.verbose) { await logger.logToStderr(`Amount of users to permanently delete: ${users.length}`); } const batchRequests = users.map((user, index) => { return { id: index, method: 'DELETE', url: `/directory/deletedItems/${user.id}` }; }); for (let i = 0; i < batchRequests.length; i += 20) { const batchRequestChunk = batchRequests.slice(i, i + 20); if (this.verbose) { await logger.logToStderr(`Deleting users: ${i + batchRequestChunk.length}/${users.length}`); } const requestOptions = { url: `${this.resource}/v1.0/$batch`, headers: { accept: 'application/json', 'content-type': 'application/json' }, responseType: 'json', data: { requests: batchRequestChunk } }; await request.post(requestOptions); } } catch (err) { this.handleRejectedODataJsonPromise(err); } }; if (args.options.force) { await clearRecycleBinUsers(); } else { const result = await cli.promptForConfirmation({ message: 'Are you sure you want to permanently delete all deleted users?' }); if (result) { await clearRecycleBinUsers(); } } } } export default new EntraUserRecycleBinItemClearCommand(); //# sourceMappingURL=user-recyclebinitem-clear.js.map