-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear-all-products.js
More file actions
48 lines (39 loc) · 1.45 KB
/
clear-all-products.js
File metadata and controls
48 lines (39 loc) · 1.45 KB
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
require('dotenv').config();
const mongoose = require('mongoose');
const Product = require('./models/Product');
async function clearAllProducts() {
try {
console.log('Connecting to MongoDB...');
console.log('URI:', process.env.MONGODB_URI ? 'Found' : 'Missing');
await mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
console.log('✅ Connected to MongoDB');
// Get count of existing products
const count = await Product.countDocuments();
console.log(`Found ${count} products in database`);
if (count > 0) {
// List all products before deletion
const products = await Product.find({}).select('_id name');
console.log('\n=== Products to be deleted ===');
products.forEach(p => {
console.log(`- ${p.name} (ID: ${p._id})`);
});
// Delete all products
const result = await Product.deleteMany({});
console.log(`\n✅ Successfully deleted ${result.deletedCount} products`);
} else {
console.log('No products found in database');
}
// Verify deletion
const finalCount = await Product.countDocuments();
console.log(`\nFinal product count: ${finalCount}`);
await mongoose.connection.close();
console.log('Database connection closed');
} catch (error) {
console.error('❌ Error:', error.message);
console.error('Full error:', error);
}
}
clearAllProducts();