Skip to content

Instantly share code, notes, and snippets.

@AlmogCohen
Forked from osdiab/_README.md
Last active February 6, 2021 21:34
Show Gist options
  • Save AlmogCohen/d487a54d9eb9dd3c6e556daf99d3e79e to your computer and use it in GitHub Desktop.
Save AlmogCohen/d487a54d9eb9dd3c6e556daf99d3e79e to your computer and use it in GitHub Desktop.
Deletes all your cached URLs in prerender

You can run this by doing the following:

  1. Log into https://prerender.io
  2. Open the developer console on the prerender site
  3. Copy the code in this gist and paste it into your console
  4. Hit enter!

Some notes:

  • This script takes a while, Prerender is not very performant! So if you have a lot of cached URLs, be patient.
  • I recommend opening the network tab of your developer console, and making sure that all the requests to /cached-pages and remove-cached-urls are returning 200 OK responses. If they're returning any response with status 400+ it's likely this script isn't working.
  • This may stop working at any time if Prerender changes their API or API usage policies ever.
(async function () {
while (true) {
// fetch pages 1000 items at a time, the Prerender max
// as of 2021/01/06; dont need to worry about page num
// because after deletion these pages will be gone anyway
const searchTerm = 'someTerm'
const getPagesResult = await fetch(
`https://prerender.io/api/cached-pages?pageSize=500&q=${searchTerm}`
);
if (getPagesResult.status > 299) {
throw new Error(
"Could not fetch cached pages; see network console for details"
);
}
const data = await getPagesResult.json();
if (data.length === 0) {
break; // we're done!
}
const removeFetchResult = await fetch(
"https://prerender.io/api/remove-cached-urls",
{
credentials: "include",
headers: {
"Content-Type": "application/json",
"X-XSRF-TOKEN": document.cookie.match(/XSRF-TOKEN=([\w\-]+)/)[1],
},
body: JSON.stringify({
urls: data.map(({ url, adaptive_type }) => ({ url, adaptive_type })),
}),
method: "DELETE",
mode: "cors",
}
);
if (removeFetchResult.status > 299) {
throw new Error(
"Could not delete cached pages; see network console for details"
);
}
}
})().then(() => console.log("done"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment