Skip to content

Instantly share code, notes, and snippets.

@nabeken
Last active August 29, 2015 14:05
Show Gist options
  • Save nabeken/14624762f23131fafe69 to your computer and use it in GitHub Desktop.
Save nabeken/14624762f23131fafe69 to your computer and use it in GitHub Desktop.
cleanup vfs directory
#!/usr/bin/env python
import json
import os
import shutil
import subprocess
import re
dockerdir = '/var/lib/docker'
volumesdir = os.path.join(dockerdir, 'volumes')
vfsdir = os.path.join(dockerdir, 'vfs', 'dir')
containers = dict((line, 1) for line in subprocess.check_output('docker ps -a -q --no-trunc', shell=True).splitlines())
volumes = os.walk(os.path.join(volumesdir, '.')).next()[1]
for volume in volumes:
if not re.match('[0-9a-f]{64}', volume):
print volume + ' is not a valid volume identifier, skipping...'
continue
volume_metadata = json.load(open(os.path.join(volumesdir, volume, 'json')))
container_id = volume_metadata['id']
if container_id in containers:
print 'Container ' + container_id[:12] + ' does still exist, not clearing up volume ' + volume
continue
print 'Deleting volume ' + volume + ' (container: ' + container_id[:12] + ')'
vfspath = os.path.join(vfsdir, volume)
print 'Vfspath: ' + vfspath
shutil.rmtree(vfspath)
volumepath = os.path.join(volumesdir, volume)
print 'Volumepath: ' + volumepath
shutil.rmtree(volumepath)
@nabeken
Copy link
Author

nabeken commented Sep 4, 2014

COMPLETELY BROKEN. DO NOT USE.

@nabeken
Copy link
Author

nabeken commented Sep 5, 2014

I've pushed docker-volume-cleanup command to https://github.com/nabeken/docker-volume-cleanup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment