MCPcopy
hub / github.com/directus/directus / recursiveDelete

Function recursiveDelete

app/src/utils/delete-folder.ts:35–62  ·  view source on GitHub ↗
(folders: Folder[], allFolders: Folder[])

Source from the content-addressed store, hash-verified

33}
34
35export async function recursiveDelete(folders: Folder[], allFolders: Folder[]): Promise<void> {
36 const allFolderIds = collectAllFolderIds(
37 allFolders,
38 folders.map((f) => f.id),
39 );
40
41 const allFolderIdSet = new Set(allFolderIds);
42
43 const withParentInSet = allFolders
44 .filter((f) => allFolderIdSet.has(f.id) && f.parent !== null && allFolderIdSet.has(f.parent!))
45 .map((f) => f.id);
46
47 if (withParentInSet.length > 0) {
48 await api.patch('/folders', { keys: withParentInSet, data: { parent: null } });
49 }
50
51 const filesRes = await api.get('/files', {
52 params: { filter: { folder: { _in: allFolderIds } }, fields: ['id'], limit: -1 },
53 });
54
55 const fileIds: string[] = filesRes.data.data.map((f: { id: string }) => f.id);
56
57 if (fileIds.length > 0) {
58 await api.delete('/files', { data: fileIds });
59 }
60
61 await api.delete('/folders', { data: allFolderIds });
62}

Callers 1

Calls 4

collectAllFolderIdsFunction · 0.90
hasMethod · 0.65
getMethod · 0.65
deleteMethod · 0.65

Tested by

no test coverage detected