Created
August 27, 2024 19:33
-
-
Save btholt/522b1e5790de65236cb5578bb52ce1cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("dotenv").config(); | |
const apiToken = process.env.VERCEL_API_TOKEN; | |
async function getVercelProjects() { | |
const apiUrl = "https://api.vercel.com/v9/projects"; | |
const apiToken = process.env.VERCEL_API_TOKEN; | |
let projectIds = []; | |
let next = null; | |
try { | |
do { | |
const url = next ? `${apiUrl}?from=${next}` : apiUrl; | |
console.log("Fetching projects from", url); | |
const response = await fetch(url, { | |
headers: { | |
Authorization: `Bearer ${apiToken}`, | |
}, | |
}); | |
if (!response.ok) { | |
throw new Error(`HTTP error! status: ${response.status}`); | |
} | |
const data = await response.json(); | |
const projects = data.projects; | |
projectIds = projectIds.concat(projects.map((project) => project.id)); | |
next = data.pagination.next; | |
console.log("Next:", next); | |
console.log("Projects:", projects.length); | |
console.log("====================================="); | |
} while (next); | |
return projectIds; | |
} catch (error) { | |
console.error("Error fetching projects:", error); | |
throw error; | |
} | |
} | |
async function deleteVercelProjects(projectIds) { | |
const apiUrl = "https://api.vercel.com/v9/projects"; | |
for (const projectId of projectIds) { | |
try { | |
const response = await fetch(`${apiUrl}/${projectId}`, { | |
method: "DELETE", | |
headers: { | |
Authorization: `Bearer ${apiToken}`, | |
}, | |
}); | |
if (!response.ok) { | |
throw new Error( | |
`Failed to delete project ${projectId}: ${response.statusText}` | |
); | |
} | |
console.log(`Successfully deleted project ${projectId}`); | |
} catch (error) { | |
console.error(`Error deleting project ${projectId}:`, error); | |
} | |
} | |
} | |
getVercelProjects() | |
.then((projectIds) => { | |
console.log("Project IDs:", projectIds); | |
return deleteVercelProjects(projectIds); | |
}) | |
.catch((error) => { | |
console.error("Error:", error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Throwing this up as a gist in case anyone needs to delete 100+ Vercel projects like I did