How to delete all files in directory on remote SFTP server in Python?

Question:

I’d like to delete all the files in a given directory on a remote server that I’m already connected to using Paramiko. I cannot explicitly give the file names, though, because these will vary depending on which version of file I had previously put there.

Here’s what I’m trying to do… the line below the #TODO is the call I’m trying where remoteArtifactPath is something like /opt/foo/*

ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(server, username=username, pkey=mykey)
sftp = ssh.open_sftp()

# TODO: Need to somehow delete all files in remoteArtifactPath remotely
sftp.remove(remoteArtifactPath+"*")

# Close to end
sftp.close()
ssh.close()

Any idea how I can achieve this?

Asked By: Cuga

||

Answers:

I found a solution: Iterate over all the files in the remote location, then call remove on each of them:

ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(server, username=username, pkey=mykey)
sftp = ssh.open_sftp()

# Updated code below:
filesInRemoteArtifacts = sftp.listdir(path=remoteArtifactPath)
for file in filesInRemoteArtifacts:
    sftp.remove(remoteArtifactPath+file)

# Close to end
sftp.close()
ssh.close()
Answered By: Cuga

A Fabric routine could be as simple as this:

with cd(remoteArtifactPath):
    run("rm *")

Fabric is great for executing shell commands on remote servers. Fabric actually uses Paramiko underneath, so you can use both if you need to.

Answered By: ianmclaury

You need a recursive routine since your remote directory may have subdirectories.

def rmtree(sftp, remotepath, level=0):
    for f in sftp.listdir_attr(remotepath):
        rpath = posixpath.join(remotepath, f.filename)
        if stat.S_ISDIR(f.st_mode):
            rmtree(sftp, rpath, level=(level + 1))
        else:
            rpath = posixpath.join(remotepath, f.filename)
            print('removing %s%s' % ('    ' * level, rpath))
            sftp.remove(rpath)
    print('removing %s%s' % ('    ' * level, remotepath))
    sftp.rmdir(remotepath)

ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(server, username=username, pkey=mykey)
sftp = ssh.open_sftp()
rmtree(sftp, remoteArtifactPath)

# Close to end
stfp.close()
ssh.close()
Answered By: markolopa

For @markolopa answer, you need 2 imports to get it working:

import posixpath
from stat import S_ISDIR
Answered By: broferek

I found a solution, using python3.7 e spur 0.3.20. It is very possible that works with others versions as well.

import spur

shell = spur.SshShell( hostname="ssh_host", username="ssh_usr", password="ssh_pwd")
ssh_session = shell._connect_ssh()

ssh_session.exec_command('rm -rf  /dir1/dir2/dir3')

ssh_session.close()
Answered By: Allan Santos

So I know this is an older post but I would still like to give a short answer that I found to be more usefull than the rest I found. Also this uses paramikos in-built functions so it should work on all devices

import paramiko

class remote_operations:
    def __init__(self):
        pass
    
    def connect(self, hostname, username, password):
        client = paramiko.SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname, username=username, password=password)
        return client

def delete_files():
    print("--Deleting files--")
    test = remote_operations()

    # these ips and passwords are just examples
    username = "aabfbkbakjdfb123"
    password = "I_am_not_good_at_making_passwords_123"
    host_ip = "111.111.11.11"

    client = test.connect(host_ip,username,password)
    sftp_client = client.open_sftp()

    folderPath = "/my_secret_files/"
    sftp_client.chdir(folderPath)

    for file in sftp_client.listdir():
        sftp_client.remove(file)

if __name__ == '__main__':
    delete_files()
Answered By: Ace-Of-Snakes
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.