Skip to content

Instantly share code, notes, and snippets.

@Byteflux
Last active October 25, 2018 10:21
Show Gist options
  • Save Byteflux/8d3aab647437c01590e48b991788dd95 to your computer and use it in GitHub Desktop.
Save Byteflux/8d3aab647437c01590e48b991788dd95 to your computer and use it in GitHub Desktop.
Gist Cleaner Script
<?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);
@Byteflux
Copy link
Author

If you have Docker installed, you can use the following command to delete all private gists on your account:

sudo docker run --rm \
                -e GITHUB_USER=Byteflux \
                -e GITHUB_PASSWORD=Password_or_Token \
                php:7.2-cli \
                sh -c 'curl -sL https://gist.github.com/Byteflux/8d3aab647437c01590e48b991788dd95/raw | php'

Replace the GITHUB_USER and GITHUB_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment