-
Notifications
You must be signed in to change notification settings - Fork 13
/
members-bulk-delete.js
85 lines (71 loc) · 2.43 KB
/
members-bulk-delete.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Members Bulk Delete
*
* Usage:
*
* node members-bulk-delete.js https://blah.ghost.io ADMIN_API_KEY [true]
*
* If you run this script with just a URL and key, it will do a dry run
* If you run this script with an extra argument (e.g. true) the deletions will be executed
*/
if (process.argv.length < 4) {
console.error('Missing an argument. Requires API_URL API_KEY [confirm]');
process.exit(1);
}
const url = process.argv[2];
const key = process.argv[3];
const doDelete = process.argv[4] === 'true';
if (!doDelete) {
console.log('Dry run...');
} else {
console.log('Will do deletions!');
}
const Promise = require('bluebird');
const GhostAdminAPI = require('@tryghost/admin-api');
const api = new GhostAdminAPI({
url,
key,
version: 'canary'
});
(async function main() {
if (doDelete) {
console.log('REAL Run');
} else {
console.log('Dry Run - nothing will be deleted');
}
console.log('API URL', url);
console.log('API KEY', key);
// Give the user time to read...
await Promise.delay(1000);
try {
const allMembers = await api.members.browse({limit: 'all'});
const keep = [];
const freeMembers = allMembers.filter((member) => {
// Comped members should have a subscription, but just in case
if (!member.comped && member.stripe.subscriptions.length === 0) {
return true;
}
keep.push(member.email);
return false;
});
console.log(freeMembers.length, 'Members will be deleted out of', allMembers.length, 'total members. This will leave', keep.length, 'members');
console.log('Keeping:');
console.log(keep);
if (doDelete) {
const result = await Promise.mapSeries(freeMembers, async (member) => {
console.log('Deleting', member.email);
// Call the API
const result = await api.members.delete({id: member.id});
// Add a delay but return the original result
return Promise.delay(50).return(result);
});
console.log('Deleted', result.length, 'members');
}
} catch (err) {
console.error('There was an error', require('util').inspect(err, false, null));
if (err.type === 'NotFoundError') {
console.log('Resource not found - is members actually enabled?');
}
process.exit(1);
}
}());