-
Notifications
You must be signed in to change notification settings - Fork 0
/
Delete Folders in Bulk.ps1
75 lines (60 loc) · 2.98 KB
/
Delete Folders in Bulk.ps1
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
# Delete Specific set of folders or files from SharePoint site
$Folder = Import-Csv "C:\Users\amand\Downloads\Folders.csv"
$SiteURL = Import-Csv "C:\Users\amand\Downloads\SiteURL.csv"
# We are considering a common root folder in this use case.
$ListName = "Documents"
#Connect to Sites recursively to and get the Library url
try {
foreach ($a in $SiteURL.url) {
Connect-PnPOnline -Url $a -Interactive -verbose
write-host $("Start time " + (Get-Date))
#site and list details
$siteID = (Get-PnPSite -Includes Id).Id
$listID = (Get-PnPList $listName).Id
$web = (get-pnpweb).ServerRelativeUrl
# Creating a batch job for bulk deletion
$batchSize = 20
#bearer token for batch request
$token = Get-PnPGraphAccessToken
#Get all the list items
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000
Write-host "Total Number of Items Found:"$ListItems.count "in Site:" $a
#Delete all files from a folder in batch permanently
foreach ($i in $Folder.Folder) {
#Construct the Serverrelative URL
$FolderServerRelativeURL = $web + "/" + $ListName + "/" + $i
#Get All Items from the csv Folders by filtering
$ItemsFromFolder = $ListItems | Where-Object { $_.FieldValues["FileDirRef"] -match $FolderServerRelativeURL }
$requests = @()
$itemCount = $ItemsFromFolder.Count
#Delete all files from a folder in batch permanently using Graph
for ($i = $itemCount - 1; $i -ge 0; $i--) {
$itemId = $ItemsFromFolder[$i].Id
$request = @{
id = $i
method = "DELETE"
url = "/sites/$siteID/lists/$listID/items/$itemId"
headers = $header
}
$requests += $request
if ($requests.count -eq $batchSize -or $requests.count -eq $itemCount) {
$batchRequests = @{
requests = $requests
}
#IMPORTANT: use -Deph parameter
$batchBody = $batchRequests | ConvertTo-Json -Depth 4
#send batch request
$response = Invoke-WebRequest -Method Post -Uri 'https://graph.microsoft.com/v1.0/$batch' -Headers @{Authorization = "Bearer $($token)" } -ContentType "application/json" -Body $batchBody
$StatusCode = $Response.StatusCode
write-host $("$StatusCode response for deleting 20")
#reset batch item counter and requests array
$requests = @()
$itemCount = $itemCount - $batchSize
}
}
}
}
}
catch {
write-host -f Red "Error" $_.Exception.Message
}