|
| 1 | +--- |
| 2 | +title: "Aumatically Bulk Delete 'Recent Documents' Shared with You on Google Docs Homescreen Using JavaScript" |
| 3 | +author: "PrashantUnity" |
| 4 | +weight: 109 |
| 5 | +date: 2026-02-24 |
| 6 | +lastmod: 2025-02-24 |
| 7 | +dateString: February 2026 |
| 8 | +description: "" |
| 9 | +#canonicalURL: "https://canonical.url/to/page" |
| 10 | +cover: |
| 11 | + # image: "devtunnel.png" # image path/url |
| 12 | + alt: "Docs" # alt text |
| 13 | + #caption: "Microsoft Dev Tunnels" display caption under cover |
| 14 | + |
| 15 | +tags: [ "google docs", "automation", "javascript", "productivity" ] |
| 16 | +keywords: [ "Google Docs", "automation", "JavaScript", "productivity", "bulk delete", "recent documents", "shared files", "codefrydev", "CFD"] |
| 17 | +--- |
| 18 | + |
| 19 | +# How to Bulk Delete "Recent Documents" shared with you on the Google Docs Homescreen Using JavaScript |
| 20 | + |
| 21 | +If you've ever tried to clean up your Google Docs homescreen by removing old or shared documents, you know how tedious it can be. Google doesn't provide a built-in way to bulk delete files from the "Recent Documents" section, and the interface is designed to make it difficult to accidentally delete files. This means you have to click through each file's 3-dot menu and select "Remove" one by one. |
| 22 | + |
| 23 | +## The Solution: A Robust Automation Script |
| 24 | + |
| 25 | +To get around these hurdles, the script needs to simulate a heavy, deliberate human mouse click, scroll elements into view so they are actually clickable, and strictly filter out any hidden HTML elements. |
| 26 | + |
| 27 | +Here is the final, working script. |
| 28 | + |
| 29 | +### How to use it |
| 30 | + |
| 31 | +1. Go to your [Google Docs homescreen](https://docs.google.com) or Google Sheets. |
| 32 | +2. Open your browser's Developer Tools (Press `F12` or `Ctrl + Shift + J` on Windows / `Cmd + Option + J` on Mac). |
| 33 | +3. Navigate to the **Console** tab. |
| 34 | +4. Paste the code below and hit **Enter**. |
| 35 | + |
| 36 | +```javascript |
| 37 | +async function removeAllDocs() { |
| 38 | + const delay = ms => new Promise(r => setTimeout(r, ms)); |
| 39 | + |
| 40 | + // Simulates a heavy, deliberate human mouse click |
| 41 | + const triggerClick = (element) => { |
| 42 | + // Bring element into view first |
| 43 | + element.scrollIntoView({ behavior: 'smooth', block: 'center' }); |
| 44 | + |
| 45 | + ['pointerdown', 'mousedown', 'pointerup', 'mouseup', 'click'].forEach(eventType => { |
| 46 | + element.dispatchEvent(new MouseEvent(eventType, { |
| 47 | + view: window, |
| 48 | + bubbles: true, |
| 49 | + cancelable: true, |
| 50 | + buttons: 1 |
| 51 | + })); |
| 52 | + }); |
| 53 | + }; |
| 54 | + |
| 55 | + let total = 0; |
| 56 | + |
| 57 | + while (true) { |
| 58 | + // 1. Find VISIBLE 3-dot menus |
| 59 | + const menuButtons = Array.from(document.querySelectorAll('[role="button"]')) |
| 60 | + .filter(btn => { |
| 61 | + const label = (btn.getAttribute('aria-label') || btn.getAttribute('data-tooltip') || "").toLowerCase(); |
| 62 | + return label.includes("rename, remove") || label.includes("more options") || label.includes("more actions"); |
| 63 | + }) |
| 64 | + .filter(btn => btn.offsetParent !== null && btn.style.display !== 'none'); |
| 65 | + |
| 66 | + if (menuButtons.length === 0) { |
| 67 | + console.log("No visible menus found. Scrolling down..."); |
| 68 | + window.scrollBy(0, 1000); |
| 69 | + await delay(2500); |
| 70 | + |
| 71 | + const retryButtons = Array.from(document.querySelectorAll('[role="button"]')) |
| 72 | + .filter(btn => { |
| 73 | + const label = (btn.getAttribute('aria-label') || btn.getAttribute('data-tooltip') || "").toLowerCase(); |
| 74 | + return label.includes("rename, remove") || label.includes("more options") || label.includes("more actions"); |
| 75 | + }) |
| 76 | + .filter(btn => btn.offsetParent !== null && btn.style.display !== 'none'); |
| 77 | + |
| 78 | + if (retryButtons.length === 0) { |
| 79 | + console.log("No more files left to process."); |
| 80 | + break; |
| 81 | + } |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + const menuBtn = menuButtons[0]; |
| 86 | + console.log("Opening menu..."); |
| 87 | + triggerClick(menuBtn); |
| 88 | + |
| 89 | + // Wait for the menu to fully pop up |
| 90 | + await delay(1200); |
| 91 | + |
| 92 | + // 2. Find the "Remove" button that is ACTUALLY VISIBLE on the screen right now |
| 93 | + const removeBtn = Array.from(document.querySelectorAll('[role="menuitem"], .goog-menuitem')) |
| 94 | + .filter(el => el.offsetParent !== null) // CRITICAL: Only grab the visible one |
| 95 | + .find(el => { |
| 96 | + const text = (el.innerText || "").toLowerCase(); |
| 97 | + return text.includes("remove") || text.includes("move to trash") || text.includes("delete"); |
| 98 | + }); |
| 99 | + |
| 100 | + if (!removeBtn) { |
| 101 | + console.log("Visible 'Remove' option not found. Closing menu and skipping..."); |
| 102 | + document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })); |
| 103 | + menuBtn.style.display = 'none'; |
| 104 | + await delay(1000); |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + // 3. Click remove |
| 109 | + console.log("Clicking remove..."); |
| 110 | + triggerClick(removeBtn); |
| 111 | + total++; |
| 112 | + console.log(`Total removed so far: ${total}`); |
| 113 | + |
| 114 | + // Hide the button we just clicked so the loop doesn't catch it again |
| 115 | + menuBtn.style.display = 'none'; |
| 116 | + |
| 117 | + // Give the server time to process the deletion |
| 118 | + await delay(2000); |
| 119 | + } |
| 120 | + |
| 121 | + console.log(`DONE. Successfully removed ${total} files.`); |
| 122 | +} |
| 123 | + |
| 124 | +removeAllDocs(); |
| 125 | +``` |
| 126 | + |
| 127 | +### Important Notes |
| 128 | + |
| 129 | +- This script is designed to work on the Google Docs homescreen and may not work on other Google Drive interfaces. |
| 130 | +- It simulates human-like interactions, so it may take some time to process all files, especially if you have many documents. |
| 131 | +- Be cautious when using this script, as it will permanently remove files from your "Recent Documents" list. Make sure you have backups of any important files before running it. |
| 132 | +- The script may need adjustments if Google updates their interface, as it relies on specific HTML structures and attributes to identify buttons and menu items. |
| 133 | +- Always test the script on a small number of files first to ensure it works as expected before running it on a larger scale. |
| 134 | + |
| 135 | +- if you encounter any issues or have suggestions for improvement, feel free to comment below or you could use ai by proving this script. and problem you are facing! |
0 commit comments