Last active
October 25, 2018 10:21
-
-
Save Byteflux/8d3aab647437c01590e48b991788dd95 to your computer and use it in GitHub Desktop.
Gist Cleaner Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Your GitHub username goes here. | |
$user = $_ENV['GITHUB_USER'] ?? ''; | |
// If using 2FA, you must create a Personal Access Token at https://github.com/settings/tokens | |
// The access token can be used in place of the password and should be revoked when no longer needed. | |
$password = $_ENV['GITHUB_PASSWORD'] ?? ''; | |
// This is the base URL to the GitHub API. | |
// If applicable, you can replace it with your custom GitHub Enterprise URL. | |
$url = $_ENV['GITHUB_API_URL'] ?? 'https://api.github.com'; | |
// Customize the filter to only delete specific gists. | |
// The default filter excludes public gists, meaning only private gists are deleted. | |
$filter = function ($gist) { | |
return !$gist['public']; | |
}; | |
/* Everything below here is the actual deletion logic. */ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Gist Cleaner Script'); | |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |
curl_setopt($ch, CURLOPT_USERPWD, "$user:$password"); | |
do { | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); | |
curl_setopt($ch, CURLOPT_URL, "$url/users/$user/gists"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$gists = array_filter(json_decode(curl_exec($ch), true), $filter); | |
if (empty($gists)) { | |
break; | |
} | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); | |
foreach ($gists as $gist) { | |
curl_setopt($ch, CURLOPT_URL, "$url/gists/$gist[id]"); | |
curl_exec($ch); | |
} | |
} while (true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you have Docker installed, you can use the following command to delete all private gists on your account:
Replace the
GITHUB_USER
andGITHUB_PASSWORD
values with your GitHub username and password, or if you have two-factor authentication enabled, you must generate an application token at https://github.com/settings/tokens to be used as the password.If you want to run this script against a GitHub Enterprise server, you can supply the API URL with the
GITHUB_API_URL
environment variable.