Google Cloud billing
How do I disable Google Cloud billing automatically when a budget is hit?
Google's documented hard stop for a Google Cloud project is a budget with a threshold at 100 percent that publishes to Pub/Sub, plus a Cloud Run function that removes the project's billing account once cost passes the budget. That terminates every service in the project, Free Tier included, and Google warns that resources might be irretrievably deleted and can take up to 24 hours to start again after you turn billing back on.
It is also late. Budget notifications arrive multiple times a day, the first can take several hours, and Google says the setup doesn't guarantee you won't spend more than your budget.
How the disable-billing recipe works
Google's tutorial wires three pieces together: a budget scoped to one project, a Pub/Sub topic the budget publishes to, and a Cloud Run function triggered by that topic. On every message the function compares costAmount with budgetAmount. If cost is higher and billing is still on, it calls the Cloud Billing API's updateProjectBillingInfo with an empty billingAccountName, which unlinks the project from its billing account.
- Enable the APIs the tutorial uses: Billing Budgets, Cloud Billing, Cloud Build, Cloud Functions, Eventarc, Cloud Run, Pub/Sub and Artifact Registry.
- Create a Pub/Sub topic.
- Deploy a Cloud Run function with that topic as its trigger, the entry point
stopBilling(Node.js) orstop_billing(Python), andGOOGLE_CLOUD_PROJECTset to the project to cut off. - Give the function's service account permission on the billing account (next section).
- Create a budget scoped to that one project with a threshold at 100 percent, connected to the topic.
- Test with a small budget first. Google's Python sample ships with
SIMULATE_DEACTIVATION = True, so it only logs until you change it.
Google's sample script, placeholders in capitals:
gcloud pubsub topics create billing-alerts-topic --project=PROJECT_ID
gcloud functions deploy stop-billing-function \
--gen2 \
--runtime=nodejs24 \
--region=us-central1 \
--trigger-topic=billing-alerts-topic \
--entry-point=stopBilling \
--set-env-vars GOOGLE_CLOUD_PROJECT=PROJECT_ID \
--source=./billing_function \
--project=PROJECT_ID
gcloud billing accounts add-iam-policy-binding BILLING_ACCOUNT_ID \
--member="serviceAccount:FUNCTION_SERVICE_ACCOUNT" \
--role="roles/billing.admin"
gcloud billing budgets create \
--billing-account=BILLING_ACCOUNT_ID \
--display-name="Budget for PROJECT_ID" \
--budget-amount=100 \
--threshold-rule=percent=100 \
--notifications-rule-pubsub-topic="projects/PROJECT_ID/topics/billing-alerts-topic"Google's Node.js function, which depends on @google-cloud/billing:
const {CloudBillingClient} = require('@google-cloud/billing');
const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT;
const PROJECT_NAME = `projects/${PROJECT_ID}`;
const billing = new CloudBillingClient();
exports.stopBilling = async pubsubEvent => {
const pubsubData = JSON.parse(
Buffer.from(pubsubEvent.data, 'base64').toString()
);
if (pubsubData.costAmount <= pubsubData.budgetAmount) {
return `No action necessary. (Current cost: ${pubsubData.costAmount})`;
}
if (!PROJECT_ID) {
return 'No project specified';
}
const billingEnabled = await _isBillingEnabled(PROJECT_NAME);
if (billingEnabled) {
return _disableBillingForProject(PROJECT_NAME);
} else {
return 'Billing already disabled';
}
};
const _isBillingEnabled = async projectName => {
try {
const [res] = await billing.getProjectBillingInfo({name: projectName});
return res.billingEnabled;
} catch (e) {
console.log(
'Unable to determine if billing is enabled on specified project, assuming billing is enabled'
);
return true;
}
};
const _disableBillingForProject = async projectName => {
const [res] = await billing.updateProjectBillingInfo({
name: projectName,
resource: {billingAccountName: ''}, // Disable billing
});
return `Billing disabled: ${JSON.stringify(res)}`;
};The permission the function needs
Google's sample script grants the function's service account Billing Account Administrator (roles/billing.admin) on the whole billing account. The tutorial text only asks for the appropriate billing account permissions, and the API reference says the caller must own the project or the billing account.
Google's billing docs list two permissions that can unlink a project: billing.resourceAssociations.delete on the billing account, which Billing Account Administrator has, or resourcemanager.projects.deleteBillingAssignment on the project, which Project Billing Manager and Project Owner have. Billing Account Administrator reaches every project on the account, so keep the function's code and its service account locked down.
What disabling billing does to a project
| Area | After billing is disabled |
|---|---|
| Services | All Google Cloud and Google Maps Platform services in the project stop, Free Tier included |
| Data and resources | Some resources might be removed and become non-recoverable; Google says to back up data first |
| Shared VPC host project | Services in service projects that depend on the Shared VPC are disabled too |
| Firebase and Google AI Studio | Firebase drops from Blaze to the no-cost Spark plan; both fall back to the limited free plans |
| The function itself | Disabled as well if it runs in the same project |
| Charges already run up | Still billed to the old billing account, and can take up to two days to show up |
| Turning it back on | Re-enable billing by hand; resources can take up to 24 hours to start and some need a manual restart |
Two cases block the function entirely: you can't disable billing on a project that is locked to a billing account, or on one with active or pending committed use discounts. For a Firebase project, see Firebase spending limit.
Why it can still overspend
The function only runs when a budget notification arrives. Google sends them multiple times per day, the first can take several hours after you create the budget, and costs reach Cloud Billing with a delay of their own. Google's advice: if your funds are limited, set the budget below what you can actually pay. More on the timing in Google Cloud budget alerts.
Pub/Sub delivers at least once, so a message can arrive twice or out of order; the sample checks whether billing is already off, so repeats do no harm. If delivery fails because the topic is misconfigured, nothing tells you.
Watch the restart. costAmount counts from the start of the budget period, and the sample disables billing on any message where it is above the budget. Turn billing back on in the same month without raising the budget or detaching the topic, and the next notification turns it off again.
The gentler option: Google spend caps
For four services, Google offers spend caps instead: a budget that pauses one service in one project once its gross estimated cost passes 100 percent of the budget, for the rest of the budget period. The four are the Gemini API, Gemini Enterprise Agent Platform (formerly Vertex AI), Cloud Run and Cloud Run functions. Enforcement isn't instant and overage is billed, persistent resources such as compute and storage keep running, and after a cap is lifted services can take up to an hour to recover. Details in Google Cloud spending limits.
For other services, Google documents a middle path: a function that stops selected resources, such as some Compute Engine VMs, and leaves the rest alone.
Where Costbrake fits
Costbrake never disables billing. Its one Google Cloud brake sets a zero spend cap on one service in one project, after an admin picks the target and confirms; nothing fires on its own. Google pauses new usage once its estimate passes the cap, which isn't instant, and usage until then is billed. Undo lifts the cap, and Google can take up to an hour to let usage through again.
Costbrake reads the Cloud Billing export to BigQuery, usually a few hours behind and sometimes more than a day, and fills the gap with live Cloud Monitoring estimates for the Gemini API, Vertex AI, Cloud Run and Cloud Run functions, about 5 minutes behind. On Pro and Team that means a check every 5 minutes, and every 2 minutes for 2 hours after a spike; the export alone is checked hourly. Brakes come with Pro and Team, and setup is on the Google Cloud integration page. Keep the disable-billing function only as a last resort, for projects where losing everything costs less than the bill.
Questions and answers
Does disabling billing on a Google Cloud project delete my data?
It can. Google says some resources might be removed and become non-recoverable while billing is disabled, and recommends backing up data first. Every service in the project stops, Free Tier included.
How fast does the disable-billing function stop Google Cloud spend?
Only as fast as budget notifications arrive. Google sends them multiple times per day, the first can take several hours, and costs reach Cloud Billing with a delay, so Google says the setup doesn't guarantee you stay under budget.
How do I turn billing back on after the function disabled it?
Re-enable billing on the project by hand in the Google Cloud console. Resources can take up to 24 hours to start again and some need a manual restart. Raise the budget or detach the Pub/Sub topic first, or the next notification disables billing again.
Can I stop Google Cloud spend without disabling billing?
For four services, yes. A spend cap pauses the Gemini API, Gemini Enterprise Agent Platform (formerly Vertex AI), Cloud Run or Cloud Run functions in one project once estimated cost passes the budget. It isn't instant, overage is billed, and persistent resources such as VMs and storage keep running.
Does Costbrake disable Google Cloud billing?
No. Costbrake's only Google Cloud brake sets a zero spend cap on one service in one project, after an admin confirms it, and Undo lifts the cap. It never unlinks a billing account.
Sources
- Disable billing usage with notifications (Google Cloud)
- Enable, disable, or change billing for a project
- Set up programmatic notifications
- Control resource usage with notifications
- Spend caps (Cloud Billing)
- Create, edit, or delete budgets and budget alerts
- projects.updateBillingInfo (Cloud Billing API)
- Avoid surprise bills (Firebase)
Updated