Skip to content

Instantly share code, notes, and snippets.

@osdiab
Last active December 28, 2022 18:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save osdiab/71b0cd8c5c7385e816130e9d597ec4cd to your computer and use it in GitHub Desktop.
Save osdiab/71b0cd8c5c7385e816130e9d597ec4cd 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 getPagesResult = await fetch(
"https://prerender.io/api/cached-pages?pageSize=1000"
);
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
.split(";")
.find((s) => s.startsWith("XSRF-TOKEN"))
.split("=")[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"));
@osdiab
Copy link
Author

osdiab commented Jan 6, 2021

Enjoy!

@AlmogCohen
Copy link

I've replaced the xsrf parsing logic as it was failing on Safari "X-XSRF-TOKEN": document.cookie.match(/XSRF-TOKEN=([\w\-]+)/)[1],

@osdiab
Copy link
Author

osdiab commented Feb 8, 2021

seems reasonable, i was just running this from chrome as a one-off script so I didn't mind browser cross compatibility - glad it works for you!

@MeisamMulla
Copy link

The API URLs have changed to https://dashboard.prerender.io/api/cached-pages?pageSize=1000 and https://dashboard.prerender.io/api/remove-cached-urls

@VjeraTurk
Copy link

VjeraTurk commented Feb 1, 2022

Thanks a ton!
I had to run the script a couple of times. Prerender.io seems to automatically upgrade/downgrade your subscription plan based on how many pages are cached.

By removing enough of them, I fell to a lower rang which I think broke the execution of remove-cached-urls.

WARNING:

New subscription plans are now additionally charging caching and re-caching pages (2 $/ 1000 recaches) keep that in mind and think about if you really want to delete the cache
image

@MeisamMulla
Copy link

I also had to add a space to the beginning of XSRF_TOKEN as follows:

.find((s) => s.startsWith(" XSRF-TOKEN"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment