about_IOInfoExtensions.PowerShell_DeleteContent.help.txt

TOPIC
    about_IOInfoExtensions.PowerShell_DeleteContent
 
SHORT DESCRIPTION
    Deletes all the content within the directory.
 
SYNTAX
    DeleteContent()
 
LONG DESCRIPTION
    This method deletes all the files and directories within the calling [DirectoryInfo] object.
 
    It does not delete the directory itself.
 
OUTPUTS
    N/A
 
EXAMPLES
    --------------------- Example 1 ---------------------
    Delete all content within a directory.
 
        $directory = New-Object System.IO.DirectoryInfo 'C:\Demo'
        $children = $directory.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories)
 
        Write-Host "Number of Children:`n`t$($children.Count)"
        Write-Host "Children:"
        $children.FullName | ForEach-Object { Write-Host "`t$_" }
 
        $directory.DeleteContent()
        $children = $directory.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories)
        Write-Host "Number of Children after delete:`n`t$($children.Count)"
        Write-Host "Root Exists:`n`t$($directory.Exists)"
 
        <#
        Output:
        Number of Children:
                6
        Children:
                C:\Demo\ChildDir1
                C:\Demo\ChildDir2
                C:\Demo\ChildFile1.txt
                C:\Demo\ChildFile2.txt
                C:\Demo\ChildDir2\ChildFile1.txt
                C:\Demo\ChildDir2\ChildFile2.txt
        Number of Children after delete:
                0
        Root Exists:
                True
        #>
 
    Recursively deletes all files and directories within the given directory but does not delete the directory itself.
 
    --------------------- Example 2 ---------------------
    Safely calls DeleteContent even if no content exists.
 
        $directory = New-Object System.IO.DirectoryInfo 'C:\Demo'
        $children = $directory.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories)
        Write-Host "Number of Children:`n`t$($children.Count)"
 
        Write-Host "Deleting content..."
        $directory.DeleteContent()
        $children = $directory.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories)
        Write-Host "Number of Children after delete:`n`t$($children.Count)"
 
        Write-Host "Deleting content..."
        $directory.DeleteContent()
        $children = $directory.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories)
        Write-Host "Number of Children after second delete:`n`t$($children.Count)"
 
        Write-Host "Root Exists:`n`t$($directory.Exists)"
 
        Write-Host "Deleting Root..."
        $directory.Delete()
        $directory.Refresh()
        Write-Host "Root Exists after delete:`n`t$($directory.Exists)"
 
        Write-Host "Deleting content..."
        $directory.DeleteContent()
        $children = $directory.GetFileSystemInfos("*", [System.IO.SearchOption]::AllDirectories)
        Write-Host "Number of Children after root delete:`n`t$($children.Count)"
 
        <#
        Output:
        Number of Children:
                6
        Number of Children after delete:
                0
        Number of Children after second delete:
                0
        Root Exists:
                True
        Deleting Root...
        Root Exists after delete:
                False
        Deleting content...
        Number of Children after root delete:
                0
        #>
 
    This demonstrates that DeleteContent can be safely called without needing to check for existence.
 
KEYWORDS
    IOInfoExtensions, IOInfoExtensions.PowerShell, System.IO.DirectoryInfo, DirectoryInfo, DeleteContent
 
SEE ALSO
    about_IOInfoExtensions.PowerShell
    about_IOInfoExtensions.PowerShell_GetDirectory
    about_IOInfoExtensions.PowerShell_GetFile
    about_IOInfoExtensions.PowerShell_DeleteContent
    about_IOInfoExtensions.PowerShell_CopyContentTo
    about_IOInfoExtensions.PowerShell_MoveFrom
    about_IOInfoExtensions.PowerShell_CopyFrom
    about_IOInfoExtensions.PowerShell_TryDelete