Skip to content

Instantly share code, notes, and snippets.

@danielberndt
Last active September 5, 2025 08:56
Show Gist options
  • Select an option

  • Save danielberndt/3899cdbcb48b49dd069acef87115cb65 to your computer and use it in GitHub Desktop.

Select an option

Save danielberndt/3899cdbcb48b49dd069acef87115cb65 to your computer and use it in GitHub Desktop.
Codecks: Batch remove files via browser console
void (async () => {
const subdomain = window.location.host.split(".")[0]
const attachmentQuery = {
createdAt: { op: "lt", value: "2024-09-04" },
file: {
isDeleted: false,
selfHosted: true,
},
$order: "createdAt",
$limit: 100
};
// build up the nested query and define which models and fields we want to receive
const query = {
_root: [
{
account: [
{
[`attachments(${JSON.stringify(attachmentQuery)})`]: [
{ file: ["id"] },
],
},
],
},
],
};
// pass the query to the api
const data = await fetch(
`https://api.codecks.io?query=${JSON.stringify(
query
)}&x-account=${subdomain}`,
{ credentials: "include" }
).then((r) => r.json());
const attachments = Object.values(data.attachment)
const body = {ids: attachments.map(a => a.id), fileIds: attachments.map(a => a.file)}
await fetch(
`https://api.codecks.io/dispatch/attachments/batchDeleteFile?&x-account=${subdomain}`,
{ credentials: "include", method: "POST", body: JSON.stringify(body), headers: {"Content-Type": "application/json"} }
)
})()
// delete any file no matter if used as attachment or not (filtered by date and file size)
void (async () => {
const subdomain = window.location.host.split(".")[0]
const fileQuery = {
createdAt: { op: "lt", value: "2024-09-04" },
isDeleted: false,
selfHosted: true,
size: {op: "gt", value: 1000000},
$order: "createdAt",
$limit: 100
};
// build up the nested query and define which models and fields we want to receive
const query = {
_root: [
{
account: [
{
[`files(${JSON.stringify(fileQuery)})`]: [
"id"
],
},
],
},
],
};
// pass the query to the api
const data = await fetch(
`https://api.codecks.io?query=${JSON.stringify(
query
)}&x-account=${subdomain}`,
{ credentials: "include" }
).then((r) => r.json());
// parse the response and create delete request
const files = Object.values(data.file)
const body = {ids: files.map(f => f.id)}
await fetch(
`https://api.codecks.io/dispatch/attachments/batchDeleteByFileId?&x-account=${subdomain}`,
{ credentials: "include", method: "POST", body: JSON.stringify(body), headers: {"Content-Type": "application/json"} }
)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment