Created
October 18, 2017 16:14
-
-
Save 4e4c52/4e6ed7260e1249a97d25b31804cea1d7 to your computer and use it in GitHub Desktop.
Batch remove your old Facebook posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You must have Ruby | |
# Install the following gems: capybara, poltergeist and koala | |
# Install phamtomjs (brew install phantomjs) | |
# Run with `ruby clean_fb.rb` | |
require 'koala' | |
require 'capybara' | |
require 'capybara/poltergeist' | |
# Create an Access Token at https://developers.facebook.com/tools/explorer (you may need to create an app) | |
TOKEN = '' | |
# Your Facebook login information | |
EMAIL = '' | |
PASSWORD = '' | |
# Remove all posts until this date | |
UNTIL_DATE = '2016-01-01' | |
def remove_posts(posts) | |
File.open('not_deleted.log', 'w') do |f| | |
posts.each do |p| | |
id = p['id'] | |
short_id = id.split('_').last | |
url = "https://mbasic.facebook.com/story.php?story_fbid=#{short_id}&id=#{short_id}" | |
puts "=> Will delete post #{id}, #{url}" | |
# Open post page | |
@session.visit url | |
begin | |
# Go to delete page | |
# It will fail if the post is a photo | |
@session.within '#u_0_0' do | |
@session.click_link "Delete" | |
end | |
# Confirm delete | |
@session.click_button "Delete" | |
puts "==> Deleted post #{id}" | |
@posts_count += 1 | |
rescue Capybara::ElementNotFound => e | |
puts "==> Can't delete post #{id}, skipping..." | |
f.puts url | |
end | |
end | |
end | |
end | |
@posts_count = 0 | |
# Signin to FB | |
@session = Capybara::Session.new(:poltergeist) | |
@session.visit "https://mbasic.facebook.com/" | |
@session.fill_in 'email', with: EMAIL | |
@session.fill_in 'pass', with: PASSWORD | |
@session.click_button 'Log In' | |
# Authenticate with the Graph API | |
@graph = Koala::Facebook::API.new(TOKEN) | |
# Get all posts until UNTIL_DATE | |
@posts = @graph.get_connection('me', 'posts', { until: UNTIL_DATE, fields: ['id'] }) | |
remove_posts(@posts) | |
until @posts.next_page.empty? | |
@posts = @posts.next_page | |
remove_posts(@posts) | |
end | |
puts "==> Deleted #{@posts_count} posts" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment