Uploadthing Delete Files

Hi, I'm trying to delete lots of files in one go using a cron job... I detect files that are more than 3 days old and delete all of them... I'm doing this:
const activityFilenamesToDelete = expiredActivityRecords.map(
(record) => record.filename,
);

const taskFilenamesToDelete = expiredTaskRecords.map(
(record) => record.evidence?.[0]?.filename,
);

const filenamesToDelete = [
...activityFilenamesToDelete,
...taskFilenamesToDelete,
];

if (filenamesToDelete.length > 0) {
try {
const validFilenamesToDelete = filenamesToDelete.filter(
(filename): filename is string => filename !== null,
);
await utapi.deleteFiles(validFilenamesToDelete);
console.log(
`Successfully deleted ${validFilenamesToDelete.length} expired files`,
);
} catch (error) {
console.error("Failed to delete expired files:", error);
}
}

return Response.json({ deletedCount: filenamesToDelete.length });
const activityFilenamesToDelete = expiredActivityRecords.map(
(record) => record.filename,
);

const taskFilenamesToDelete = expiredTaskRecords.map(
(record) => record.evidence?.[0]?.filename,
);

const filenamesToDelete = [
...activityFilenamesToDelete,
...taskFilenamesToDelete,
];

if (filenamesToDelete.length > 0) {
try {
const validFilenamesToDelete = filenamesToDelete.filter(
(filename): filename is string => filename !== null,
);
await utapi.deleteFiles(validFilenamesToDelete);
console.log(
`Successfully deleted ${validFilenamesToDelete.length} expired files`,
);
} catch (error) {
console.error("Failed to delete expired files:", error);
}
}

return Response.json({ deletedCount: filenamesToDelete.length });
  filename is the key of each, some are like this: "S3NUJszt9cEpdYb3l7F8xZGXwMvl7CkJ0qUjcYnNgiWe2mHb", some others like "ba8c9b9a-cda1-47b5-a972-ee1936c27a6c-wy6d.gif", i don't know why it's failing, but it may be because i'm trying to delete over 20k files... it doesn't throw an error, it just doesn't delete anything. please help
1 Reply
feldrok
feldrokOP2mo ago
{"message":"Failed to request UploadThing API (400)","logLevel":"ERROR","timestamp":"2024-10-16T04:08:28.535Z","annotations":{"response":{"_id":"@effect/platform/HttpClientResponse","request":{"_id":"@effect/platform/HttpClientRequest","method":"POST","url":"https://api.uploadthing.com/v6/deleteFiles","urlParams":[],"hash":{"_id":"Option","_tag":"None"},"headers":{"content-type":"application/json","content-length":"888416","x-uploadthing-version":"7.1.0","x-uploadthing-be-adapter":"server-sdk","x-uploadthing-api-key":"xx","b3":"xx","traceparent":"00-ac1748f7c8d5387b42008b683c2beeb2"},"body":{"_id":"@effect/platform/HttpBody","_tag":"Uint8Array","body":"{\"fileKeys\":[\"S3NUJszt9cEpo0fqIqCiQCRPDxlaIc8F9MpZS4uNwGr0Jg3e\",\"S3NUJszt9cEpiVWd2JsMoK3mjplcaAYBwd49tQqn8EyP0xZT\",\"S3NUJszt9cEp8RxwogaUItnwQOfDLFuvqi3JpAgk9r1mc7la\",
{"message":"Failed to request UploadThing API (400)","logLevel":"ERROR","timestamp":"2024-10-16T04:08:28.535Z","annotations":{"response":{"_id":"@effect/platform/HttpClientResponse","request":{"_id":"@effect/platform/HttpClientRequest","method":"POST","url":"https://api.uploadthing.com/v6/deleteFiles","urlParams":[],"hash":{"_id":"Option","_tag":"None"},"headers":{"content-type":"application/json","content-length":"888416","x-uploadthing-version":"7.1.0","x-uploadthing-be-adapter":"server-sdk","x-uploadthing-api-key":"xx","b3":"xx","traceparent":"00-ac1748f7c8d5387b42008b683c2beeb2"},"body":{"_id":"@effect/platform/HttpBody","_tag":"Uint8Array","body":"{\"fileKeys\":[\"S3NUJszt9cEpo0fqIqCiQCRPDxlaIc8F9MpZS4uNwGr0Jg3e\",\"S3NUJszt9cEpiVWd2JsMoK3mjplcaAYBwd49tQqn8EyP0xZT\",\"S3NUJszt9cEp8RxwogaUItnwQOfDLFuvqi3JpAgk9r1mc7la\",
Want results from more Discord servers?
Add your server