2

I need to delete list from a site collection using power shell commands. Can anyone help me with the code.

[Actual requirement : Delete all list which starts with "xyz"]

1 Answer 1

8

Here it is:

$startsWith = "ABC"
$site = Get-SPSite http://mycoolsite
$webs = $site.AllWebs

foreach ($web in $webs) {
    $lists = $web.Lists
    for ($index = 0; $index -lt $lists.Count; $index++) {
        if ($lists[$index].Title.StartsWith($startsWith)) {
            $lists[$index].Delete()
        }
    }
    $web.Dispose()
}

$site.Dispose()
3
  • can you also provide the generalized code to delete list with list name specified. Commented Sep 6, 2011 at 12:18
  • @MonicaJagani - for this generalized code: Do you know web location or do you need to search all webs in site collection to find it? You can easily modify provided code to do second: just use 'if($lists[$index].Title -eq $startsWith)'. This will delete list with name defined in $StartsWith Commented Sep 6, 2011 at 12:30
  • It works better if you iterate backwards while deleting; the listed code didn't quite work for me - for ($index = $lists.Count - 1; $index -ge 0; $index--) { Commented Mar 21, 2013 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.