I have a function with the following code. Its purpose is to delete old deployments and win back storage capacity (since there's no option in the Console that I know of... please point me towards it if there is one <3)
import {serverFunctions} from '#/functions/appwriteClient.ts';
import type {IFunctionContext} from '#/functions/models.ts';
import {Query} from 'node-appwrite';
export default async ({res}: IFunctionContext) => {
const functions = await serverFunctions.list();
const deletionPromises: Promise<object>[] = [];
console.log(`Found ${functions.total} functions. Deleting old deployments...`);
for (const function_ of functions.functions) {
const deploymentList = await serverFunctions.listDeployments(function_.$id, [Query.equal('activate', false)]);
const deletableDeployments = deploymentList.deployments
.map((deployment) => ({...deployment, $createdAt: new Date(deployment.$createdAt)}))
.sort((a, b) => {
const aSuccessful = a.status !== 'failed' ? 1 : 0;
const bSuccessful = b.status !== 'failed' ? 1 : 0;
if (!aSuccessful || !bSuccessful) {
return bSuccessful - aSuccessful;
}
return b.$createdAt.getTime() - a.$createdAt.getTime();
});
for (const deployment of deletableDeployments.slice(3)) {
console.log(`Deleting deployment ${deployment.$id} for function ${function_.$id}`);
deletionPromises.push(serverFunctions.deleteDeployment(function_.$id, deployment.$id));
}
}
await Promise.all(deletionPromises);
return res.empty();
};