You shouldn't do anything directly in the content database. This can lead to many problems and is unsupported and discouraged by Microsoft. If you want to use PowerShell, you have several methods to do this. Generally, you can get list items using CAML query with Contains condition and export information about them to CSV for verification. Then you can use batch processing to delete them in one request.
$webUrl = ''
$listName = ''
$query = '<Where>' +
'<Contains>' +
'<FieldRef Name="FileLeafRef"/><Value Type="Text">bad_file</Value>' +
'</Contains>' +
'</Where>'
$viewFields = '<FieldRef Name="FileRef"/><FieldRef Name="FileLeafRef"/>'
$delete = $false
Start-SPAssignment -Global
try {
$web = Get-SPWeb $webUrl
$list = $web.Lists[$listName]
$spQuery = New-Object -TypeName Microsoft.SharePoint.SPQuery
$spQuery.Query = $query
$spQuery.ViewFields = $viewFields
$spQuery.ViewFieldsOnly = $true
$spQuery.ViewAttributes = 'Scope="Recursive"'
$items = $list.GetItems($spQuery)
$items | Select-Object ID, @{Name="FileName";Expression={$_["FileLeafRef"]}} |
Export-Csv -Path ".\FilesToDelete.csv" -NoTypeInformation
if ($delete) {
$batch = '<?xml version="1.0" encoding="UTF-8"?>'
$batch += '<ows:Batch>'
foreach ($item in $items) {
$batch += '<Method>'
$batch += '<SetList Scope="Request">' + $list.ID + '</SetList>'
$batch += '<SetVar Name="Cmd">Delete</SetVar>'
$batch += '<SetVar Name="ID">' + $item.ID + '</SetVar>'
$batch += '<SetVar Name="owsfileref">' + $item["FileRef"] + '</SetVar>'
$batch += '</Method>'
}
$batch += '</ows:Batch>'
$web.ProcessBatchData($batch)
}
}
catch {
Write-Error $_
}
Stop-SPAssignment -Global
Batch delete moves files to Recycle Bin, so if you want to delete them permanently, you may want to erase Recycle Bin manually or using code:
$web.RecycleBin.DeleteAll()
$web.Site.RecycleBin.DeleteAll()
Instead batch delete, you could just loop through list items and call Delete()
method on each, but this would be slower.