A Python tool to identify and archive orphaned files in Google Drive.
This tool helps you clean up your Google Drive by finding and archiving "orphaned" files - files that are not located in any folder. These files often accumulate over time when files are removed from folders without being deleted, or when shared folders are unshared.
Orphaned files are files in your Google Drive that:
- Have no parent folder (not located in any directory)
- Are owned by you
- Are not in trash
These files typically occur when:
- Files are removed from folders but not deleted
- Shared folders are removed, leaving files behind
- Files are created at the root level without a folder
- Folder structures are reorganized and files are left behind
- Safe Operation: Files are moved to trash (not permanently deleted) and can be restored within 30 days
- User Confirmation: Displays the list of orphaned files and requires confirmation before archiving
- OAuth2 Authentication: Secure authentication using Google's OAuth2 flow
- Pagination Support: Efficiently handles large Google Drive accounts
- Graceful Interruption: Supports Ctrl+C to exit cleanly at any time
- Python 3.7 or higher
- A Google account with Google Drive
- Google Cloud Console project with Drive API enabled
-
Clone this repository:
git clone https://github.com/yourusername/gdrive-archive.git cd gdrive-archive -
Install required Python packages:
pip install -r requirements.txt
- Go to the Google Cloud Console
- Create a new project or select an existing one
- Enable the Google Drive API:
- Navigate to "APIs & Services" > "Library"
- Search for "Google Drive API"
- Click "Enable"
- In Google Cloud Console, go to "APIs & Services" > "Credentials"
- Click "Create Credentials" > "OAuth client ID"
- Configure the OAuth consent screen if prompted
- Select "Desktop app" as the application type
- Give it a name (e.g., "gdrive-archive")
- Click "Create"
- Download the JSON file
Copy the downloaded client_secret.json file to:
~/.credentials/gdrive-archive/client_secret.jsonCreate the directory if it doesn't exist:
mkdir -p ~/.credentials/gdrive-archive
cp /path/to/downloaded/client_secret_*.json ~/.credentials/gdrive-archive/client_secret.jsonRun the script:
python gdrive_archive.pyThe script will:
- Open your browser for Google authentication (first run only)
- Search your Google Drive for orphaned files
- Display the count of orphaned files found
- List each orphaned filename
- Wait for you to press Enter to confirm
- Archive (trash) the files
--noauth_local_webserver: Use this flag if running on a remote/headless server. It will provide a URL for you to visit for authentication.
Example:
python gdrive_archive.py --noauth_local_webserver- Authentication: Uses OAuth2 to securely access your Google Drive
- Search: Queries all files in your Drive (excluding trash) and filters for:
- Files with no parent folder
- Files owned by you
- Review: Shows you the list of orphaned files
- Archive: After confirmation, moves files to trash using the Drive API
- Non-destructive: Files are moved to trash, not permanently deleted
- Recoverable: Trashed files can be restored from Google Drive trash for 30 days
- User-controlled: Requires explicit confirmation before archiving
- Read-only until confirmed: No changes are made until you press Enter to confirm
Make sure you've placed the OAuth2 credentials file at:
~/.credentials/gdrive-archive/client_secret.json
Delete the stored token and re-authenticate:
rm ~/.credentials/gdrive-archive/user_token.json
python gdrive_archive.pyIf the script reports 0 orphaned files, your Drive is already well-organized! All your files are in folders.
See LICENSE file for details.
Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.
Please read SECURITY.md for details on our security policy and how to report security vulnerabilities.
Please read CODE_OF_CONDUCT.md for details on our code of conduct.