from cloudpathlib import CloudPath
# create a few files to work with
cl1 = CloudPath("s3://test-bucket/so/test_dir/f1.txt")
cl2 = CloudPath("s3://test-bucket/so/test_dir/f2.txt")
cl3 = CloudPath("s3://test-bucket/so/test_dir/f3.txt")
# write content to these files
cl1.write_text("hello file 1")
cl2.write_text("hello file 2")
cl3.write_text("hello file 3")
# show these file exist on S3
list(CloudPath("s3://test-bucket/so/test_dir/").iterdir())
#> [ S3Path('s3://test-bucket/so/test_dir/f1.txt'),
#> S3Path('s3://test-bucket/so/test_dir/f2.txt'),
#> S3Path('s3://test-bucket/so/test_dir/f3.txt')]
# remove a single file with `unlink`
cl1.unlink()
list(CloudPath("s3://test-bucket/so/test_dir/").iterdir())
#> [ S3Path('s3://test-bucket/so/test_dir/f2.txt'),
#> S3Path('s3://test-bucket/so/test_dir/f3.txt')]
# remove a directory with `rmtree`
CloudPath("s3://test-bucket/so/test_dir/").rmtree()
# no more files
list(CloudPath("s3://test-bucket/so/").iterdir())
#> []