I had the pleasure of having to delete several hundreds of sitemaps from different Google Search Console properties (please do not ask for the reason).
I have been directed to this article, written by Chris Gee.
Since I am more of a coder than a Google Sheets user, I opted to create my own little Python script to do the job. And to make your life easier, I decided to share it with you. Am I not a nice person? 😀
Get your bearer token for the GSC API
I have simply followed the steps described by Chris Gee in his article, which is very straight forward.
Step 1: Go to https://developers.google.com/oauthplayground/
Step 2: Choose Google Search Console for the product
Step 3: Choose https://www.googleapis.com/auth/webmasters (NOT readonly)
Step 4: Click the blue „Authorize APIs“ button and log in with the account that has access to the GSC property/ properties you need to access.
Step 5: Click the blue „Exchange authorization code for tokens“ button to get your bearer token
Step 6: After clicking that button, you will see your token in the access_token property of the code.
Copy that access_token – you will need it later in the code.
Set up your environment
Create a new folder on your computer. Also, make sure you have Python installed.
Gather the sitemap URLs you want to delete
In your new folder, create a file called sitemaps_to_delete.txt and paste the URLs of the sitemaps you want to delete. Make sure to double check them, as all sitemaps in this file will be deleted from your GSC property.
Your file should look something like this
https://your-domain.com/sitemap-to-delete-1.xml
https://your-domain.com/sitemap-to-delete-2.xml
https://your-domain.com/sitemap-to-delete-3.xml
https://your-domain.com/sitemap-to-delete-4.xml
....
Code-Sprache: JavaScript (javascript)
The Python Script
In the same folder, create a new python file. You can call it delete_sitemaps.py.
Copy and paste the following code into a new python file.
Changes you need to make to this script:
- Edit the value of SITEMAPS_FILE to the filename of the file containing the list of sitemap URLs.
- Edit the value of BEARER_TOKEN – paste the token your have copied earlier.
import requests
from urllib.parse import quote
SITEMAPS_FILE = "sitemaps_to_delete.txt"
API_URL = "https://www.googleapis.com/webmasters/v3/sites/{site_url}/sitemaps/{sitemap_url}"
BEARER_TOKEN = "YOUR_TOKEN" # Insert your token here
def delete_sitemap(site_url, sitemap_url):
encoded_sitemap_url = quote(sitemap_url, safe="")
url = API_URL.format(site_url=quote(site_url, safe=""), sitemap_url=encoded_sitemap_url)
headers = {
"Authorization": f"Bearer {BEARER_TOKEN}",
}
response = requests.delete(url, headers=headers)
if response.status_code == 204:
print(f"Sitemap has been deleted: {sitemap_url}")
else:
print(f"Error while deleting sitemap: {sitemap_url}. Response: {response.status_code} - {response.text}")
def main():
site_url = input("Insert your GSC property domain (i.e. https://example.com or sc-domain:example.com): ")
try:
with open(SITEMAPS_FILE, "r") as file:
sitemaps = file.readlines()
for sitemap in sitemaps:
sitemap_url = sitemap.strip()
if sitemap_url:
delete_sitemap(site_url, sitemap_url)
except FileNotFoundError:
print(f"File {SITEMAPS_FILE} could not be found.")
if __name__ == "__main__":
main()
Code-Sprache: PHP (php)
Open your terminal and navigate to your project folder. Then run the command
python delete_sitemaps.py
Code-Sprache: CSS (css)
You will see something like this. Simply enter your GSC property’s domain and hit Enter.
You will see that the sitemaps are getting deleted one by one. The changes should be seen in your GSC property in real time (sitemaps are gone).
Hope this helps. If it does, le tme know in the comments. If you have any additions to the script – also let me know 🙂