fastify-xstorage
delete / deleteMultiple
Delete a single file or batch-delete many files via the S3 DeleteObjects API.
delete / deleteMultiple
Delete one file by key, or batch-delete many files in a single S3 DeleteObjects call. deleteMultiple returns a count of successful deletes and any per-key errors without throwing.
Signatures
fastify.xStorage.delete(key: string): Promise<true>
fastify.xStorage.deleteMultiple(
keys: string[]
): Promise<{ deleted: number; errors: Array<{ Key: string; Message: string }> }>
Params
delete
| Name | Type | Required | Description |
|---|---|---|---|
key | string | Yes | The storage key of the file to delete. |
deleteMultiple
| Name | Type | Required | Description |
|---|---|---|---|
keys | string[] | Yes | Non-empty array of storage keys to delete. |
Returns
delete
true on success.
deleteMultiple
| Field | Type | Description |
|---|---|---|
deleted | number | Number of files successfully deleted. |
errors | array | Per-key errors from S3. Empty on full success. |
Throws
[xStorage] key is required and must be a string—delete()called without a valid key.[xStorage] keys must be a non-empty array of strings—deleteMultiple()called with empty or non-array.[xStorage] Failed to delete file "<key>": <message>—delete()S3 error.[xStorage] Failed to delete multiple files: <message>—deleteMultiple()S3 request error (not per-key errors).
Examples
delete — remove a single file
await fastify.xStorage.delete("avatars/user-123-a1b2c3d4.jpg");
console.log("File deleted");
deleteMultiple — batch delete
const { deleted, errors } = await fastify.xStorage.deleteMultiple([
"uploads/doc-1.pdf",
"uploads/doc-2.pdf",
"uploads/doc-3.pdf",
]);
console.log(`Deleted ${deleted} of 3 files`);
if (errors.length > 0) {
console.warn("Some deletes failed:", errors);
}
Realistic — delete user files on account removal
fastify.delete("/users/:userId", async (request, reply) => {
const { userId } = request.params;
const userFiles = await db.files.findByUser(userId);
const keys = userFiles.map((f) => f.storageKey);
if (keys.length > 0) {
const { deleted, errors } = await fastify.xStorage.deleteMultiple(keys);
fastify.log.info(`Deleted ${deleted}/${keys.length} files for user ${userId}`);
if (errors.length > 0) {
fastify.log.warn({ errors }, "Some files failed to delete");
}
}
await db.users.delete(userId);
return reply.send({ deleted: true });
});
See also
- upload / uploadMultiple — upload files to the bucket
- list / listAll — list keys to delete
- exists — check if a file still exists after deletion
AI Context
package: "@xenterprises/fastify-xstorage"
methods: fastify.xStorage.delete(key) | fastify.xStorage.deleteMultiple(keys)
use-when: Delete one file or batch-delete many files from S3-compatible storage
returns: delete → true | deleteMultiple → { deleted: number, errors: array }
