Skip to content

Instantly share code, notes, and snippets.

@timw255
Created July 1, 2026 14:52
Show Gist options
  • Select an option

  • Save timw255/af5e741b8c9badbf976e808eaa076470 to your computer and use it in GitHub Desktop.

Select an option

Save timw255/af5e741b8c9badbf976e808eaa076470 to your computer and use it in GitHub Desktop.
Delete Aprimo File Version via API
import { createClient, Expander } from 'aprimo-js';
import type { Record, File, FileVersion } from 'aprimo-js/model';
import * as dotenv from 'dotenv';
dotenv.config();
const RECORD_ID = '817087a08d1246f4b3b0b478013eeec8';
// The version number of the file version to remove (e.g. 1).
const VERSION_NUMBER_TO_REMOVE = 2;
async function deleteFileVersion() {
const aprimo = createClient({
type: 'client_credentials',
environment: process.env.APRIMO_ENVIRONMENT!,
clientId: process.env.APRIMO_CLIENT_ID!,
clientSecret: process.env.APRIMO_CLIENT_SECRET!,
});
try {
// Expand files and their versions so we can locate the version to remove.
const expander = Expander.create()
.for<Record>('Record').expand('files')
.for<File>('File').expand('fileversions');
console.log(`Fetching record: ${RECORD_ID}`);
const res = await aprimo.records.getById(RECORD_ID, expander);
if (!res.ok) {
throw new Error(
`Failed to fetch record: ${res.error?.message || 'Unknown error'}`
);
}
const files = res.data?._embedded?.files?.items ?? [];
// Find the file that owns the version we want to remove.
let targetFile: File | undefined;
let targetVersion: FileVersion | undefined;
for (const file of files) {
const versions = file._embedded?.fileversions?.items ?? [];
const match = versions.find(
(v: FileVersion) => v.versionNumber === VERSION_NUMBER_TO_REMOVE
);
if (match) {
targetFile = file;
targetVersion = match;
break;
}
}
if (!targetFile || !targetVersion) {
throw new Error(
`Could not find a file version with versionNumber ${VERSION_NUMBER_TO_REMOVE}`
);
}
console.log(
`Removing file version v${targetVersion.versionNumber} ` +
`(${targetVersion.id}) from file ${targetFile.id}`
);
// Remove the version by nesting a `versions.remove` under the owning file.
const removePayload = {
files: {
addOrUpdate: [
{
id: targetFile.id,
versions: {
remove: [
{
id: targetVersion.id,
},
],
},
},
],
},
};
const removeResponse = await aprimo.records.update(RECORD_ID, removePayload);
if (!removeResponse.ok) {
throw new Error(
`Failed to remove file version: ${
removeResponse.error?.message || 'Unknown error'
}`
);
}
console.log('File version removed successfully');
} catch (error) {
console.error('Error:', error);
}
}
deleteFileVersion();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment