Keep your Google Drive healthy with Diet-GDrive—automatically trim the digital fat and keep only what matters!
Diet-GDrive is a smart, scriptable tool to keep your Google Drive lean and clutter-free. Delete old files, keep only your most important items, and automate cloud organization in just a few clicks. No more wasted storage. No more manual sorting. Just a healthier, lighter Drive.
- Delete all but the latest N files in one or more Google Drive folders.
- Recursively clean all subfolders (optional).
- Filter files by extension or regex pattern.
- Only delete files older than X minutes.
- Sort files by modified time, created time, name, or size.
- Dry run/test mode to preview what will be deleted.
- Verbose and logging options.
- Safe confirmation prompts (unless overridden).
- Flexible command-line usage for automation.
-
Clone this repository (or download the script file):
git clone https://github.com/ChocoJaYY/Diet-GDrive.git cd Diet-GDrive
-
Install required dependencies:
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
-
Set up Google Drive API credentials:
- Go to Google Cloud Console.
- Create a new project (if needed).
- Enable the Google Drive API.
- Create OAuth Desktop credentials and download
client_secrets.json
. - Rename your credentials file to
cleaner_client_secrets.json
and place it in the script directory.
Run the script with Python:
python DietGDrive.py [options] <folder_id> [folder_id2 ...] <files_to_keep>
<folder_id>
: One or more Google Drive folder IDs to clean.<files_to_keep>
: Number of most recent files to keep in each folder.
Option | Description |
---|---|
-t , --test-mode |
Dry run/test mode (do not delete files, just print what would happen). |
--sort |
Sort files by modifiedTime , createdTime , name , or size . |
--reverse |
Reverse the sorting order (keep oldest files instead of newest, etc). |
--ext |
Only consider files with these extensions (e.g. .pdf,.jpg ). |
--exclude |
Regex pattern for file names to exclude from deletion. |
-v , --verbose |
Verbose output. |
--logfile |
Write logs to a file (UTF-8). |
-y , --yes |
Do not prompt for confirmation, just delete. |
-r , --recursive |
Clean folders recursively (include all subfolders). |
--older-than |
Only delete files older than this many minutes. |
python DietGDrive.py YOUR_FOLDER_ID 2BcDeFgHijKLmNOpq 2
python DietGDrive.py --sort size --reverse YOUR_FOLDER_ID 20
python DietGDrive.py -r -y YOUR_FOLDER_ID 1
python DietGDrive.py --sort createdTime YOUR_FOLDER_ID 5
python DietGDrive.py --ext .jpg,.png YOUR_FOLDER_ID 15
python DietGDrive.py --exclude "(final|report)" YOUR_FOLDER_ID 3
python DietGDrive.py --test-mode --logfile cleanup.log YOUR_FOLDER_ID 7
python DietGDrive.py -r -v --ext .docx,.xlsx YOUR_FOLDER_ID 0
python DietGDrive.py --older-than 1440 YOUR_FOLDER_ID 2
python DietGDrive.py --exclude "^(?!.*_example\.).*" YOUR_FOLDER_ID 0
(Deletes all files ending with _example.*
)
python DietGDrive.py --exclude "_example\." YOUR_FOLDER_ID 0
(Keeps files ending with _example.*
, deletes all others)
python DietGDrive.py -r --exclude "^(?!.*backup).*" YOUR_FOLDER_ID 0
python DietGDrive.py --exclude "important" YOUR_FOLDER_ID 0
(Keeps files with "important" in the name, deletes all others)
On first run, a browser window will open for you to log in and grant access.
A token.json
file will be created for future use—keep this file safe.
Use --logfile mylog.txt
to log all actions, errors, and summaries to a file.
- Test with
--test-mode
before actual deletion! - The script does not touch files in the Google Drive "trash".
- You must have permission to delete files in the folders you specify.
- Recursive operations on large folders may be subject to Google API quotas.
- If you see authentication errors, delete
token.json
and re-run to re-authenticate. - If you get
quota
orrate limit
errors, wait a while or reduce the number of operations. - Check file/folder IDs and permissions if nothing happens.
PRs and suggestions are welcome! Want a new feature or better regex samples? Open an issue!
MIT License. See LICENSE for details.
Made with ❤️ to keep your cloud in shape!